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

前端 未结 7 533
予麋鹿
予麋鹿 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 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 list)
    

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

     sort(List list, Comparator c)
    

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

提交回复
热议问题