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

前端 未结 7 574
予麋鹿
予麋鹿 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:54

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

    public static > void sort(List 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 temp = new ArrayList(collection);
    Collections.sort(temp);
    collection = new HashSet(temp);
    

    Hope this helps.

提交回复
热议问题