如何将特定的 HashMap 条目移动到映射的末尾?
如何将特定的HashMap
条目移动到最后一个位置?
例如,我有像这样的 HashMap
值:
HashMap<String,Integer> map = new HashMap<String,Integer>();
// map = {Not-Specified 1, test 2, testtest 3};
“Not-Specified”可能出现在任何位置。它可能出现在地图的最前面或中间。但我想把“未指定”移到最后一个位置。
我怎样才能做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
用一句话回答您的问题:
默认情况下,地图没有最后一个条目,这不是合同的一部分。
旁注:针对接口而不是实现进行编码是一种很好的做法类(请参阅 Joshua Bloch 的《Effective Java》,第 8 章,第 52 项:Effective Java strong>通过接口引用对象)。
所以你的声明应该这样写:(
所有地图共享一个公共契约,因此客户端不需要知道它是什么类型的地图,除非他指定带有扩展契约的子接口)。
可能的解决方案
Sorted Maps:
有一个子接口 SortedMap< /a> 使用基于顺序的查找方法扩展了地图接口,它有一个子接口 NavigableMap 进一步扩展了它。该接口的标准实现,TreeMap ,允许您按自然顺序对条目进行排序(如果它们实现 可比较接口)或通过提供的 比较器。
您可以通过 访问最后一个条目lastEntry 方法:
链接映射:
还有 LinkedHashMap,一个 HashMap 实现,存储键插入的顺序。在引入 List:
Java 21 中的 SequencedMap 接口为 LinkedHashMap 提供了 lastEntry 方法获取最后一个条目:
正确的解决方案:
因为您不这样做如果不控制插入顺序,您应该使用 NavigableMap 接口,即您将编写一个比较器,将
Not-Specified
条目放在最后。这是一个示例:
输出:
使用 HashMap 的解决方案:
如果必须依赖 HashMap,仍然有一个解决方案,使用 a) 上述比较器的修改版本,b) a List 使用地图的 entrySet 和 c) Collections.sort() 辅助方法:
输出:
To answer your question in one sentence:
Per default, Maps don't have a last entry, it's not part of their contract.
And a side note: it's good practice to code against interfaces, not the implementation classes (see Effective Java by Joshua Bloch, Chapter 8, Item 52: Refer to objects by their interfaces).
So your declaration should read:
(All maps share a common contract, so the client need not know what kind of map it is, unless he specifies a sub interface with an extended contract).
Possible Solutions
Sorted Maps:
There is a sub interface SortedMap that extends the map interface with order-based lookup methods and it has a sub interface NavigableMap that extends it even further. The standard implementation of this interface, TreeMap, allows you to sort entries either by natural ordering (if they implement the Comparable interface) or by a supplied Comparator.
You can access the last entry through the lastEntry method:
Linked maps:
There is also the special case of LinkedHashMap, a HashMap implementation that stores the order in which keys are inserted. Prior to the introduction of SequencedMap in Java 21, there was however no interface to back up this functionality, nor was there a direct way to access the last key. You could only do it through tricks such as using a
List
in between:The SequencedMap interface in Java 21 provides LinkedHashMap with the lastEntry method to get the last entry:
Proper Solution:
Since you don't control the insertion order, you should go with the NavigableMap interface, i.e. you would write a comparator that positions the
Not-Specified
entry last.Here is an example:
Output:
Solution using HashMap:
If you must rely on HashMaps, there is still a solution, using a) a modified version of the above comparator, b) a List initialized with the Map's entrySet and c) the Collections.sort() helper method:
Output:
HashMap 没有“最后一个位置”,因为它没有排序。
您可以使用其他实现
java.util.SortedMap
的Map
,最流行的一个是TreeMap
。HashMap doesn't have "the last position", as it is not sorted.
You may use other
Map
which implementsjava.util.SortedMap
, most popular one isTreeMap
.SortedMap
是逻辑/最佳选择,但是另一种选择是使用LinkedHashMap
它维护两种顺序模式,最近添加的放在最后,以及最近添加的访问最后。有关更多详细信息,请参阅 Javadocs。A
SortedMap
is the logical/best choice, however another option is to use aLinkedHashMap
which maintains two order modes, most-recently-added goes last, and most-recently-accessed goes last. See the Javadocs for more details.当使用数字作为键时,我想你也可以尝试这个:
When using numbers as the key, I suppose you could also try this:
move 对于 hashmap 来说没有意义,因为它是一个字典,其中包含用于基于键进行分桶的哈希码,然后是一个通过 equals 解决冲突哈希码的链表。
使用 TreeMap 进行排序映射,然后传入自定义比较器。
move does not make sense for a hashmap since its a dictionary with a hashcode for bucketing based on key and then a linked list for colliding hashcodes resolved via equals.
Use a TreeMap for sorted maps and then pass in a custom comparator.
在这种情况下,最后使用的密钥通常是已知的,因此它可以用于访问最后一个值(与该值一起插入):
In such scenario last used key is usually known so it can be used for accessing last value (inserted with the one):
HashMap
明确未定义键顺序。根据类 Javadocs :许多其他映射类型确实定义了键顺序,包括
LinkedHashMap
和实现SortedMap
接口。 Java 21 引入了SequencedMap
地图接口,例如具有明确定义的遭遇顺序的地图。根据 其 Javadocs< /a>,
SequencedMap
是:SequencedMap
上的操作之一是putLast
。对于支持它的SequencedMap
类型,此可选操作会在映射的末尾插入一个条目,或者如果键已存在,则将其移动到末尾。LinkedHashMap
支持此方法,但SortedMap
不支持。每 方法 Javadocs:相关场景
将上述内容应用于您的相关场景,我们得到:
HashMap
explicitly does not define a key order. Per the class Javadocs:A number other map types do define a key order, including
LinkedHashMap
and maps which implement theSortedMap
interface. Java 21 introduced theSequencedMap
interface for maps such as these which have a a well-defined encounter order.Per its Javadocs, a
SequencedMap
is:One of the operations on
SequencedMap
isputLast
. This optional operation, forSequencedMap
types that support it, inserts an entry at the end of the map, or moves it to the end if the key is already present.LinkedHashMap
supports this method, butSortedMap
does not. Per the method Javadocs:Scenario in question
Applying the above to your scenario in question, we get: