jdk/src/share/classes/java/util/Collections.java
changeset 22285 6c67737a5ac0
parent 22078 bdec5d53e98c
child 23746 ce60f7b62312
equal deleted inserted replaced
22284:b2b1848b52ab 22285:6c67737a5ac0
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   119      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   119      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   120      * not be reordered as a result of the sort.
   120      * not be reordered as a result of the sort.
   121      *
   121      *
   122      * <p>The specified list must be modifiable, but need not be resizable.
   122      * <p>The specified list must be modifiable, but need not be resizable.
   123      *
   123      *
   124      * <p>Implementation note: This implementation is a stable, adaptive,
   124      * @implNote
   125      * iterative mergesort that requires far fewer than n lg(n) comparisons
   125      * This implementation defers to the {@link List#sort(Comparator)}
   126      * when the input array is partially sorted, while offering the
   126      * method using the specified list and a {@code null} comparator.
   127      * performance of a traditional mergesort when the input array is
       
   128      * randomly ordered.  If the input array is nearly sorted, the
       
   129      * implementation requires approximately n comparisons.  Temporary
       
   130      * storage requirements vary from a small constant for nearly sorted
       
   131      * input arrays to n/2 object references for randomly ordered input
       
   132      * arrays.
       
   133      *
       
   134      * <p>The implementation takes equal advantage of ascending and
       
   135      * descending order in its input array, and can take advantage of
       
   136      * ascending and descending order in different parts of the same
       
   137      * input array.  It is well-suited to merging two or more sorted arrays:
       
   138      * simply concatenate the arrays and sort the resulting array.
       
   139      *
       
   140      * <p>The implementation was adapted from Tim Peters's list sort for Python
       
   141      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
       
   142      * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
       
   143      * Sorting and Information Theoretic Complexity", in Proceedings of the
       
   144      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
       
   145      * January 1993.
       
   146      *
       
   147      * <p>This implementation dumps the specified list into an array, sorts
       
   148      * the array, and iterates over the list resetting each element
       
   149      * from the corresponding position in the array.  This avoids the
       
   150      * n<sup>2</sup> log(n) performance that would result from attempting
       
   151      * to sort a linked list in place.
       
   152      *
   127      *
   153      * @param  <T> the class of the objects in the list
   128      * @param  <T> the class of the objects in the list
   154      * @param  list the list to be sorted.
   129      * @param  list the list to be sorted.
   155      * @throws ClassCastException if the list contains elements that are not
   130      * @throws ClassCastException if the list contains elements that are not
   156      *         <i>mutually comparable</i> (for example, strings and integers).
   131      *         <i>mutually comparable</i> (for example, strings and integers).
   157      * @throws UnsupportedOperationException if the specified list's
   132      * @throws UnsupportedOperationException if the specified list's
   158      *         list-iterator does not support the {@code set} operation.
   133      *         list-iterator does not support the {@code set} operation.
   159      * @throws IllegalArgumentException (optional) if the implementation
   134      * @throws IllegalArgumentException (optional) if the implementation
   160      *         detects that the natural ordering of the list elements is
   135      *         detects that the natural ordering of the list elements is
   161      *         found to violate the {@link Comparable} contract
   136      *         found to violate the {@link Comparable} contract
       
   137      * @see List#sort(Comparator)
   162      */
   138      */
   163     @SuppressWarnings("unchecked")
   139     @SuppressWarnings("unchecked")
   164     public static <T extends Comparable<? super T>> void sort(List<T> list) {
   140     public static <T extends Comparable<? super T>> void sort(List<T> list) {
   165         Object[] a = list.toArray();
   141         list.sort(null);
   166         Arrays.sort(a);
       
   167         ListIterator<T> i = list.listIterator();
       
   168         for (Object e : a) {
       
   169             i.next();
       
   170             i.set((T) e);
       
   171         }
       
   172     }
   142     }
   173 
   143 
   174     /**
   144     /**
   175      * Sorts the specified list according to the order induced by the
   145      * Sorts the specified list according to the order induced by the
   176      * specified comparator.  All elements in the list must be <i>mutually
   146      * specified comparator.  All elements in the list must be <i>mutually
   181      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   151      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   182      * not be reordered as a result of the sort.
   152      * not be reordered as a result of the sort.
   183      *
   153      *
   184      * <p>The specified list must be modifiable, but need not be resizable.
   154      * <p>The specified list must be modifiable, but need not be resizable.
   185      *
   155      *
   186      * <p>Implementation note: This implementation is a stable, adaptive,
   156      * @implNote
   187      * iterative mergesort that requires far fewer than n lg(n) comparisons
   157      * This implementation defers to the {@link List#sort(Comparator)}
   188      * when the input array is partially sorted, while offering the
   158      * method using the specified list and comparator.
   189      * performance of a traditional mergesort when the input array is
       
   190      * randomly ordered.  If the input array is nearly sorted, the
       
   191      * implementation requires approximately n comparisons.  Temporary
       
   192      * storage requirements vary from a small constant for nearly sorted
       
   193      * input arrays to n/2 object references for randomly ordered input
       
   194      * arrays.
       
   195      *
       
   196      * <p>The implementation takes equal advantage of ascending and
       
   197      * descending order in its input array, and can take advantage of
       
   198      * ascending and descending order in different parts of the same
       
   199      * input array.  It is well-suited to merging two or more sorted arrays:
       
   200      * simply concatenate the arrays and sort the resulting array.
       
   201      *
       
   202      * <p>The implementation was adapted from Tim Peters's list sort for Python
       
   203      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
       
   204      * TimSort</a>).  It uses techniques from Peter McIlroy's "Optimistic
       
   205      * Sorting and Information Theoretic Complexity", in Proceedings of the
       
   206      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
       
   207      * January 1993.
       
   208      *
       
   209      * <p>This implementation dumps the specified list into an array, sorts
       
   210      * the array, and iterates over the list resetting each element
       
   211      * from the corresponding position in the array.  This avoids the
       
   212      * n<sup>2</sup> log(n) performance that would result from attempting
       
   213      * to sort a linked list in place.
       
   214      *
   159      *
   215      * @param  <T> the class of the objects in the list
   160      * @param  <T> the class of the objects in the list
   216      * @param  list the list to be sorted.
   161      * @param  list the list to be sorted.
   217      * @param  c the comparator to determine the order of the list.  A
   162      * @param  c the comparator to determine the order of the list.  A
   218      *        {@code null} value indicates that the elements' <i>natural
   163      *        {@code null} value indicates that the elements' <i>natural
   221      *         <i>mutually comparable</i> using the specified comparator.
   166      *         <i>mutually comparable</i> using the specified comparator.
   222      * @throws UnsupportedOperationException if the specified list's
   167      * @throws UnsupportedOperationException if the specified list's
   223      *         list-iterator does not support the {@code set} operation.
   168      *         list-iterator does not support the {@code set} operation.
   224      * @throws IllegalArgumentException (optional) if the comparator is
   169      * @throws IllegalArgumentException (optional) if the comparator is
   225      *         found to violate the {@link Comparator} contract
   170      *         found to violate the {@link Comparator} contract
       
   171      * @see List#sort(Comparator)
   226      */
   172      */
   227     @SuppressWarnings({"unchecked", "rawtypes"})
   173     @SuppressWarnings({"unchecked", "rawtypes"})
   228     public static <T> void sort(List<T> list, Comparator<? super T> c) {
   174     public static <T> void sort(List<T> list, Comparator<? super T> c) {
   229         Object[] a = list.toArray();
   175         list.sort(c);
   230         Arrays.sort(a, (Comparator)c);
       
   231         ListIterator<T> i = list.listIterator();
       
   232         for (Object e : a) {
       
   233             i.next();
       
   234             i.set((T) e);
       
   235         }
       
   236     }
   176     }
   237 
   177 
   238 
   178 
   239     /**
   179     /**
   240      * Searches the specified list for the specified object using the binary
   180      * Searches the specified list for the specified object using the binary
  4462      *
  4402      *
  4463      * <p>This example illustrates the type-safe way to obtain an empty list:
  4403      * <p>This example illustrates the type-safe way to obtain an empty list:
  4464      * <pre>
  4404      * <pre>
  4465      *     List&lt;String&gt; s = Collections.emptyList();
  4405      *     List&lt;String&gt; s = Collections.emptyList();
  4466      * </pre>
  4406      * </pre>
  4467      * Implementation note:  Implementations of this method need not
  4407      *
  4468      * create a separate <tt>List</tt> object for each call.   Using this
  4408      * @implNote
  4469      * method is likely to have comparable cost to using the like-named
  4409      * Implementations of this method need not create a separate <tt>List</tt>
  4470      * field.  (Unlike this method, the field does not provide type safety.)
  4410      * object for each call.   Using this method is likely to have comparable
       
  4411      * cost to using the like-named field.  (Unlike this method, the field does
       
  4412      * not provide type safety.)
  4471      *
  4413      *
  4472      * @param <T> type of elements, if there were any, in the list
  4414      * @param <T> type of elements, if there were any, in the list
  4473      * @return an empty immutable list
  4415      * @return an empty immutable list
  4474      *
  4416      *
  4475      * @see #EMPTY_LIST
  4417      * @see #EMPTY_LIST