LinkedHashMap 显然是一个有序的 Map。它根据插入进行排序。
那么为什么它不实现 SortedMap 呢?
最佳答案
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
虽然排序后的 map 是
A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.
所以两者的存在是为了不同的目的,其中 LinkedHashMap 以相同的键插入顺序提供迭代,而 SortedMap 则用于使用 Comparator 或 Comparable 进行排序
关于java - 为什么LinkedHashMap没有实现SortedMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629160/