is collections.sort method only used for List type of collections?

前端 未结 7 534
予麋鹿
予麋鹿 2021-01-06 03:37

friends, I am new to Java-Collection. I want to ask does Collections.sort() method only used for/by collections which are List type. I was unable t

相关标签:
7条回答
  • 2021-01-06 03:53

    Java collections has different semantics.

    • List is a set of objects that may include equals or even same elements with the order defined;

    • Set is a set of objects that doesn't include equal and same elements and doesn't specify the order;

    • SortedSet is the same as set except for it defines element order - they are stored in the order determined by compareTo() or comparator;

    Thus, the only type of collection for which sorting makes sense is List.

    0 讨论(0)
  • 2021-01-06 03:54

    The error is because the Collections class only supports a List.

    public static <T extends Comparable<? super T>> void sort(List<T> list)
    

    To sort your collection, you can try something like:

    Collection collection=new HashSet();
    collection.add("zebra");
    collection.add("frog");
    collection.add("bison");
    collection.add("puma");
    ArrayList<String> temp = new ArrayList<String>(collection);
    Collections.sort(temp);
    collection = new HashSet(temp);
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-06 03:54

    Collections.sort method expects a collection that implements a List<T> interface. This makes sense, because lists have a defined sequence. In other words, your program has complete control of the order of what goes in the list.

    Unlike lists, sets are either unordered, or specify their own, specific ordering. That is why trying to sort a Set does not make sense: it is the set, not your program, that defines the ordering of the items in the set collection. Your program can provide the logic to customize that ordering, but you cannot take an existing set, and force a different order on its items from the outside.

    If you would like to get all items of a set collection in a sorted order, copy your set into an array list, and sort the results.

    0 讨论(0)
  • 2021-01-06 04:03

    No. There is by definition no way to sort a HashSet. You can use a TreeSetinstead tho'.

    0 讨论(0)
  • 2021-01-06 04:03

    My first step with these questions is to look at Java's documentation, which will help you a lot in understanding and debugging things.

    http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

    If you look at the sort method, there, it is expecting a List.

     sort(List<T> list)
    

    It has another overriden version, but this also needs a list, as well as a comparator.

     sort(List<T> list, Comparator<? super T> c)
    

    This is why you are getting an error. There is not a method defined for sort (HashSet)

    0 讨论(0)
  • 2021-01-06 04:03

    sort(List<T> list) of Collections is the original method which expects of type List. But To sort Set basically we use TreeSet, which sorts elements by the help of compare() or compareTo() methods of Comparator and Comparable interfaces respectively.

    0 讨论(0)
提交回复
热议问题