Collections vs Arrays regarding sort()

前端 未结 8 2078
北荒
北荒 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.

提交回复
热议问题