Why is there no SortedList in Java?

后端 未结 12 948
猫巷女王i
猫巷女王i 2020-11-22 04:15

In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java Collections framework and provide a sorted way to access the elements.

However, in

12条回答
  •  情话喂你
    2020-11-22 05:11

    Set and Map are non-linear data structure. List is linear data structure.


    The tree data structure SortedSet and SortedMap interfaces implements TreeSet and TreeMap respectively using used Red-Black tree implementation algorithm. So it ensure that there are no duplicated items (or keys in case of Map).

    • List is already maintains an ordered collection and index-based data structure, trees are no index-based data structures.
    • Tree by definition cannot contain duplicates.
    • In List we can have duplicates, so there is no TreeList(i.e. no SortedList).
    • List maintains elements in insertion order. So if we want to sort the list we have to use java.util.Collections.sort(). It sorts the specified list into ascending order, according to the natural ordering of its elements.

提交回复
热议问题