java - 为什么LinkedHashMap没有实现SortedMap?

标签 java hashmap sortedmap

LinkedHashMap 显然是一个有序的 Map。它根据插入进行排序。

那么为什么它不实现 SortedMap 呢?

最佳答案

来自Java docs

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/

相关文章:

java - 根据键属性转换给定 SortedMap 的排序顺序

java - 在一个类中调用 getName() 方法有效,但在另一个类中则无效

java - 如何使用数据库单元初始化数据库(语句中未找到表)?

Java 项目 : Make HashMap (including Load-Store) Performance Better

scala - Spark : Cannot add RDD elements into a mutable HashMap inside a closure

当静态类型为 Map 时,Scala SortedMap.map 方法返回未排序的 map

java - 我什么时候应该实现比较器?

java - socket编程中如何从Server(server.java)socket持续监听PHP(client.php)socket

java - 使用内在锁时,JVM 如何确定实例?

java - 无法编码可序列化数据的值(value)