Could you please explain why the following code compiles and prints [1, 2, 3, 4], as expected. I'm using Java 8.
List nums = Arrays.asList(4, 3, 2, 1);
Collections.sort(nums);
System.out.println(nums);
As I understand, four Integer instances is created here. Each list entry contains an Object reference to an Integer instance. Since the Object class doesn't implement the Comparable interface, then Collections.sort should throw ClassCastException or something like this because it cannot cast Object references to Comparable references.
Could you please point out what I'm missing?
sort() method to sort a list of objects using some examples. By default, the sort() method sorts a given list into ascending order (or natural order). We can use Collections. reverseOrder() method, which returns a Comparator, for reverse sorting.
To sort a list of ints, floats, strings, chars or any other class that has implemented the __cmp__ method can be sorted just by calling sort on the list. If you want to sort the list in reverse order(descending), just pass in the reverse parameter as well.
Collections class provides static methods for sorting the elements of a collection. If collection elements are of a Set type, we can use TreeSet. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.
With 1,2,3,4 you are creating int
literals. While passing them to asList(T... a)
they get boxed into Integer
objects which implements Comparable
(public final class Integer extends Number implements Comparable<Integer>
), so you can sort them.
Update
Comment: Yes, but the List is declared as List, so it's the synonym to List<Object>
, not List<Integer>
, and Object
doesn't implement Comparable
.
Answer: You do not specify a generic type for the list and the Collections.sort()
method only checks if the object's class extends Comparable
. If the list has no type, your compiler should only give you a warning and everything should work fine since Integer
's are comparable.
The source code of the sort method
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
Update
Execute this piece of code to see what happens if the class does not implements Comparable
.
public class Test
{
public static void main(String[] args)
{
List objs = new ArrayList<>();
objs.add(new Test());
objs.add(new Test());
Collections.sort(objs);
}
}
The cast to Comparable
which is done in Line 290 of ComparableTimSort.class
will fail!
Exception in thread "main" java.lang.ClassCastException: src.Test cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at src.Test.main(Test.java:14)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With