Collections.sort implementation

后端 未结 5 1905
灰色年华
灰色年华 2021-02-08 18:38

I can not understand the method implementation and the logic of Collections.sort method. Here is the method implementation i found,

public static 

        
5条回答
  •  不要未来只要你来
    2021-02-08 19:04

    The way Collections.sort works is that it actually takes the collection's underlying array, and calls its sort method to sort the actual elements. That sorting algorithm used by Java is the lightning-fast Timsort.

    The method returns void because it sorts the collection in-place. That is, it modifies the collection you give it as a parameter by sorting its elements. Returning a sorted copy would be a waste of resources.

提交回复
热议问题