I have added some Integer
s to an ArrayList
of object type, and want it to be sorted. My code looks like:
List<Object> list = new ArrayList<Object>();
list.add(24);
list.add(2);
list.add(4);
Collections.sort(list); // getting error here
System.out.println("Sorted list ");
for (Object e : list) {
System.out.println(e);
}
I got the following compile-time error:
error : should implement java.lang.Compareble <? super java.lang.Object>
How should I resolve this issue?
Object
class doesn't implement Comparable
interface. If you're sure you're adding Integer
you can use code as below and then perform sorting.
List<Integer> list = new ArrayList<Integer>();
From sort() method docs
Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface . Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
The error message that your IDE generating is
The inferred type Object is not a valid substitute for the bounded parameter >
Which means that the Objects being put in that List must implement Comparable interface to accept in sort() method.
Object
class not implementing comparable interface, hence the error which you are seeing.
Using Object of type in Generic is not advisable and use specific type. Since you are adding all integers to the list just change your declaration to
List<Object> intList = new ArrayList<Object>();
If any other object of your own type, just implement comparable interface in that class or pass a custom comparator as a second parameter to sort.
Instead of doing Collections.sort(list)
, you can loop through the array and sort the objects from least to greatest.
you can do it like this:
for(int i = 0; i < intList.size(); i++) {
// if an integer is larger than any of the ones after it in the array, switch them.
}
Since you have declared your list to have the type List<Object>
, you are able to store anything into it, be it comparable or not.
The generic method Collections.sort(List)
has a type signature which requires that your list has an element type which implement the Comparable
interface, which ensures that all elements can be compared to each other, and it tells the sort
method how to compare these elements, as said interface
contains the method which can be called to compared two elements . In other words, it does not accept a List
that could contain anything.
So is your case, you should change the declaration to
List<Integer> list = new ArrayList<>();
as you are only adding Integer
objects. Integer
is a type which implements Comparable
as integer values have a natural order.
Note that you can simplify your code:
List<Integer> list = Arrays.asList(24, 2, 4);
Collections.sort(list);
System.out.println("Sorted list "+list);
The list returned by Arrays.asList
does not support changing its size, but reordering the elements is supported, hence you can sort that list.
As a side note, in the rare case, you have a List<Object>
whose type you can't change, but you know for sure that it contains only elements being naturally comparable to each other, you can circumvent the type constraint of Collection.sort
:
Collections.sort(list, null);
The method Collections.sort(List, Comparator)
supports arbitrary element types as the second parameter tells how to compare them. As a special case, a comparator of null
mandates natural order , but null
passes every type check. But, of course, using this trick will backfire when the assumption about the elements is wrong.
Generally, you should ensure that the element type as declared at compile-type is appropriate for the desired operation. Here, using List<Integer>
when the list is supposed to contain Integer
s is the right way.
the Object
class does not implement the Comperable
interface, hence it gives you this error. You should rather define it as List<Integer>
, or define a custom comperator class and pass it as an aditional Argument.
public class Comp<T> implements Comparator<T>{
@Override
public int compare(T o1, T o2) {
if(o1 instanceof Integer && o2 instanceof Integer) {
int a = (Integer) o1;
int b = (Integer) o2;
return a-b;
}
return 0;
}
}
// Call it as
Collections.sort(list, new Comp<Object>());
But you may run in several Problems while using a List of Objects and a custom Comperator, since you could add everyting to this list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.