简体   繁体   English

使用Collections.sort对对象ArrayList进行排序

[英]Sorting Object ArrayList using Collections.sort

I have added some Integer s to an ArrayList of object type, and want it to be sorted. 我向对象类型的ArrayList中添加了一些Integer ,并希望对其进行排序。 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. Object类未实现Comparable接口。 If you're sure you're adding Integer you can use code as below and then perform sorting. 如果确定要添加Integer ,则可以使用以下代码,然后执行排序。

List<Integer> list = new ArrayList<Integer>();

From sort() method docs 来自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 . 列表中的所有元素必须实现Comparable接口 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). 此外,列表中的所有元素必须相互可比较(也就是说,对于列表中的任何元素e1和e2,e1.compareTo(e2)均不得抛出ClassCastException)。

The error message that your IDE generating is 您的IDE生成的错误消息是

The inferred type Object is not a valid substitute for the bounded parameter > 推断的类型Object不是边界参数的有效替代>

Which means that the Objects being put in that List must implement Comparable interface to accept in sort() method. 这意味着放在该列表中的对象必须实现Comparable接口才能在sort()方法中接受。

Object class not implementing comparable interface, hence the error which you are seeing. Object类未实现可比较的接口,因此会出现错误。

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. 无需执行Collections.sort(list) ,而是可以遍历数组并将对象从最小到最大排序。

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. 由于您已声明列表的类型为List<Object> ,因此可以存储任何内容,无论是否可比较。

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 . 通用方法Collections.sort(List)具有类型签名,该签名要求您的列表具有实现Comparable接口的元素类型,以确保所有元素都可以相互比较,并告诉sort方法如何比较这些元素元素,因为所述interface包含可以比较两个元素的方法 In other words, it does not accept a List that could contain anything. 换句话说,它不接受可能包含任何内容的List

So is your case, you should change the declaration to 因此,您的情况是,应将声明更改为

List<Integer> list = new ArrayList<>();

as you are only adding Integer objects. 因为您只添加Integer对象。 Integer is a type which implements Comparable as integer values have a natural order. Integer是一种实现Comparable的类型,因为整数值具有自然顺序。

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. Arrays.asList返回的列表不支持更改其大小,但是支持对元素进行重新排序,因此您可以对该列表进行排序。


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 : 附带说明一下,在极少数情况下,您有一个List<Object>其类型不能更改,但是您可以确定它只包含可以自然地彼此比较的元素,因此可以避开Collection.sort的类型约束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. 方法Collections.sort(List, Comparator)支持任意元素类型,因为第二个参数告诉您如何比较它们。 As a special case, a comparator of null mandates natural order , but null passes every type check. 作为一种特殊情况, null的比较器会强制采用自然顺序 ,但null会通过每个类型检查。 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. 通常,应确保在compile-type声明的元素类型适合所需的操作。 Here, using List<Integer> when the list is supposed to contain Integer s is the right way. 在这里,当列表应该包含Integer时,使用List<Integer>是正确的方法。

the Object class does not implement the Comperable interface, hence it gives you this error. Object类未实现Comperable接口,因此会出现此错误。 You should rather define it as List<Integer> , or define a custom comperator class and pass it as an aditional Argument. 您应该将其定义为List<Integer> ,或者定义一个自定义的编译器类,并将其作为附加参数传递。

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. 但是,使用对象列表和自定义Comperator时可能会遇到一些问题,因为可以将所有内容添加到此列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM