JDK-7008270 : (coll) Collections.sort() should special case for ArrayList
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2010-12-21
  • Updated: 2014-07-22
  • Resolved: 2014-07-22
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Collections.sort() copies the passed in Collection in an array and sorts this array via Arrays.sort().

If an ArrayList is passed in it should sort on the backing array of that ArrayList directly to avoid an additional memory copy.

JUSTIFICATION :
For larger lists this additional memory copy costs time. Esp with the new improved Arrays.sort() for nearly sorted lists.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Collections.sort() for an ArrayList should have the same runtime and memory requirements as sorting the equivalent array directly.
ACTUAL -
Additional memory is required, and data is copied twice even when the passed in collection is already sorted.

CUSTOMER SUBMITTED WORKAROUND :
No direct work around possible because access to the backing array of ArrayList is private, but reflection could be used to access that array directly.

ArrayList could be reimplemented in user code to allow accessing the array.

Comments
Collections.sort(Comparator<? super E>) is now available which does not copy the array.
22-07-2014