Collections vs Arrays regarding sort()

前端 未结 8 2001
北荒
北荒 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条回答
  • 2021-01-31 04:04

    Arrays.sort() sorts Arrays i.e. Objects that are in continuous memory locations. It works on array input.

    Collections.sort() can sort objects on both continuous and discrete memory locations i.e. It can work on both ArrayList and LinkedList. Collections.sort() internally converts List into Array of objects and calls Arrays.sort() to sort them. It always create extra copy of original list objects. For ArrayList as input, it creates extra copy to maintain mutability of original array. For LinkedList as input, a new array is created for better performance purpose.

    0 讨论(0)
  • 2021-01-31 04:08

    Collection.sort is used when you dealing with lists and arrays.sort is sued when dealing with arrays. But internally Collection.sort uses Arrays.sort method only. Now internally sorting is done based in Timosrt technique instead of merge sort , because stable, adaptive arrays takes O(nlogn) comparisons in Merge sort but in Timsort worst case it will take O(nlogn) and in worst case storage requies n/2 in Timsort but it is not in the case of Merge sort technique.

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