Collections vs Arrays regarding sort()

前端 未结 8 2004
北荒
北荒 2021-01-31 03:20

Collections vs Arrays regarding sort() What is the difference between these two regarding sort() method? I know Arrays\' sort() is using binary search for sort(), what about Co

8条回答
  •  -上瘾入骨i
    2021-01-31 03:49

    As the other answers have said, you would use Collections.sort() when dealing with an object that implements the Collection interface and the Arrays.sort() method when dealing with an Array.

    A related question is what type of data structures are better if you want to sort a set of values. If you need to use a List, then I would suggest using a LinkedList since insertions run in O(1) where something like an ArrayList would be O(n).

    You could also opt for using a SortedSet if there will be no duplicates or having duplicates is unwanted. That way you don't have to bother with using an external sort method.

提交回复
热议问题