Why is there no SortedList in Java?

后端 未结 12 946
猫巷女王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:04

    Since all lists are already "sorted" by the order the items were added (FIFO ordering), you can "resort" them with another order, including the natural ordering of elements, using java.util.Collections.sort().

    EDIT:

    Lists as data structures are based in what is interesting is the ordering in which the items where inserted.

    Sets do not have that information.

    If you want to order by adding time, use List. If you want to order by other criteria, use SortedSet.

提交回复
热议问题