Is it faster to add to a collection then sort it, or add to a sorted collection?

前端 未结 7 1596
忘掉有多难
忘掉有多难 2020-11-28 04:36

If I have a Map like this:

HashMap map;

and I want to obtain a collection of values sorted us

7条回答
  •  离开以前
    2020-11-28 05:04

    Collections.sort uses mergeSort which has O(nlog n).

    TreeSet has Red-Black tree underlying, basic operations has O(logn). Hence n elements has also O(nlog n).

    So both are same big O algorithm.

提交回复
热议问题