实现Map并保持插入顺序的Java类?
我正在java中寻找一个具有键值关联但不使用哈希的类。 这是我当前正在做的事情:
- 向
Hashtable
添加值。 - 获取
Hashtable.entrySet()
的迭代器。 - 迭代所有值并:
- 获取迭代器的
Map.Entry
。 - 根据值创建
Module
类型的对象(自定义类)。 - 将类添加到 JPanel。
- 获取迭代器的
- 显示面板。
问题是我无法控制取回值的顺序,因此我无法按给定顺序显示值(无需对顺序进行硬编码)。
我将使用 ArrayList
或 Vector
来实现此目的,但在代码的后面,我需要获取给定 Key 的 Module
对象,我将其不能使用 ArrayList
或 Vector
。
有谁知道有一个免费/开源的 Java 类可以做到这一点,或者有一种根据添加时间从 Hashtable
中获取值的方法吗?
谢谢!
I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing:
- Add values to a
Hashtable
. - Get an iterator for the
Hashtable.entrySet()
. - Iterate through all values and:
- Get a
Map.Entry
for the iterator. - Create an object of type
Module
(a custom class) based on the value. - Add the class to a JPanel.
- Get a
- Show the panel.
The problem with this is that I do not have control over the order that I get the values back, so I cannot display the values in the a given order (without hard-coding the order).
I would use an ArrayList
or Vector
for this, but later in the code I need to grab the Module
object for a given Key, which I can't do with an ArrayList
or Vector
.
Does anyone know of a free/open-source Java class that will do this, or a way to get values out of a Hashtable
based on when they were added?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我建议
LinkedHashMap
或TreeMap
< /a>.LinkedHashMap
按插入顺序保留键,而TreeMap
通过Comparator
或自然的Comparable< 保持排序/code> 键的顺序。
由于它不必保持元素排序,因此在大多数情况下
LinkedHashMap
应该更快;TreeMap
对于containsKey
、get
、put
具有O(log n)
性能根据 Javadocs,remove
,而LinkedHashMap
对于每个操作都是O(1)
。如果您的 API 仅需要可预测的排序顺序,而不是特定的排序顺序,请考虑使用这两个类实现的接口,
NavigableMap
或SortedMap
。 这将使您不会将特定的实现泄漏到 API 中,并在之后随意切换到这些特定的类或完全不同的实现。I suggest a
LinkedHashMap
or aTreeMap
. ALinkedHashMap
keeps the keys in the order they were inserted, while aTreeMap
is kept sorted via aComparator
or the naturalComparable
ordering of the keys.Since it doesn't have to keep the elements sorted,
LinkedHashMap
should be faster for most cases;TreeMap
hasO(log n)
performance forcontainsKey
,get
,put
, andremove
, according to the Javadocs, whileLinkedHashMap
isO(1)
for each.If your API that only expects a predictable sort order, as opposed to a specific sort order, consider using the interfaces these two classes implement,
NavigableMap
orSortedMap
. This will allow you not to leak specific implementations into your API and switch to either of those specific classes or a completely different implementation at will afterwards.当您迭代映射的 keySet()、entrySet() 或 value() 时,LinkedHashMap 将按照元素插入映射的顺序返回元素。
这将按照元素放入地图的顺序打印元素:
LinkedHashMap will return the elements in the order they were inserted into the map when you iterate over the keySet(), entrySet() or values() of the map.
This will print the elements in the order they were put into the map:
如果不可变映射适合您的需求,那么 google 就有一个名为 guava 的库(另请参阅番石榴问题)
Guava 提供了一个 ImmutableMap 具有可靠的用户指定的迭代顺序。 此 ImmutableMap 具有containsKey、get 的 O(1) 性能。 显然不支持放置和删除。
ImmutableMap 对象是使用优雅的静态便捷方法 of() 和 copyOf() 或 Builder 对象。
If an immutable map fits your needs then there is a library by google called guava (see also guava questions)
Guava provides an ImmutableMap with reliable user-specified iteration order. This ImmutableMap has O(1) performance for containsKey, get. Obviously put and remove are not supported.
ImmutableMap objects are constructed by using either the elegant static convenience methods of() and copyOf() or a Builder object.
您可以使用 LinkedHashMap 来控制 Map 中的插入顺序。
Java LinkedHashMap 类的要点是:
它仅包含唯一元素。
LinkedHashMap 包含基于键的值。
它可能有一个空键和多个空值。
与HashMap相同,只是维护插入顺序
但如果您想使用用户定义的对象或任何原始数据类型键对映射中的值进行排序,那么您应该使用 TreeMap 有关更多信息,请参阅 此链接
You can use LinkedHashMap to main insertion order in Map
The important points about Java LinkedHashMap class are:
It contains only unique elements.
A LinkedHashMap contains values based on the key.
It may have one null key and multiple null values.
It is same as HashMap instead maintains insertion order
But if you want sort values in map using User-defined object or any primitive data type key then you should use TreeMap For more information, refer this link
您可以维护
Map
(用于快速查找)和List
(用于排序),但LinkedHashMap
可能是最简单的。 您还可以尝试SortedMap
例如TreeMap
,它具有您指定的任何顺序。You can maintain a
Map
(for fast lookup) andList
(for order) but aLinkedHashMap
may be the simplest. You can also try aSortedMap
e.g.TreeMap
, which an have any order you specify.您可以使用
LinkedHashMap
或者您可以实现自己的 CustomMap 来维护插入顺序。您可以使用具有以下功能的
CustomHashMap
:null
或空字符串的键。HashMap
与LinkedHashMap
与CustomHashMap
的使用
CustomHashMap
:O/P:
如果您知道 KEY 是固定的,那么您可以使用 EnumMap。 获取属性/XML 文件 EX 中的值
:
Either You can use
LinkedHashMap<K, V>
or you can implement you own CustomMap which maintains insertion order.You can use the Following
CustomHashMap
with the following features:null
or empty strings are not allowed.HashMap
vsLinkedHashMap
vsCustomHashMap
Usage of
CustomHashMap
:O/P:
If you know the KEY's are fixed then you can use EnumMap. Get the values form Properties/XML files
EX:
我不知道它是否是开源的,但经过一番谷歌搜索后,我发现 使用 ArrayList 实现 Map。 它似乎是 1.5 之前的 Java,因此您可能想要对其进行通用化,这应该很容易。 请注意,此实现具有 O(N) 访问权限,但如果您不向 JPanel 添加数百个小部件(无论如何您都不应该这样做),那么这应该不是问题。
I don't know if it is opensource, but after a little googling, I found this implementation of Map using ArrayList. It seems to be pre-1.5 Java, so you might want to genericize it, which should be easy. Note that this implementation has O(N) access, but this shouldn't be a problem if you don't add hundreds of widgets to your JPanel, which you shouldn't anyway.
每当我需要维护提前知道的事物的自然顺序时,我都会使用 EnumMap
键将是枚举,您可以按您想要的任何顺序插入,但当您迭代时,它将按枚举顺序(自然顺序)迭代。
此外,当使用 EnumMap 时,不应发生冲突,这样会更有效。
我确实发现使用 enumMap 可以生成干净可读的代码。
这是一个 示例
Whenever i need to maintain the natural order of things that are known ahead of time, i use a EnumMap
the keys will be enums and you can insert in any order you want but when you iterate it will iterate in the enum order (the natural order).
Also when using EnumMap there should be no collisions which can be more efficient.
I really find that using enumMap makes for clean readable code.
Here is an example
LinkedHashMap 用于维护插入顺序。 Collections框架的LinkedHashMap类是Map接口的Hashtable和LinkedList实现。 它将其条目存储在哈希表中,并在内部使用双向链表来维护插入顺序。
例如:查找 FirstNonRepeatingCharacter 的 Java 程序。 在下面的示例中,第一个非重复字符是“t”。
代码示例 1:
代码示例 2:
在代码示例 1 中,我使用 LinkedHashMap 来维护插入,从而获得预期结果,而在代码示例 2 中,我们不维护任何插入订单因此失败。
LinkedHashMap is used for maintaing insertion order. The LinkedHashMap class of the Collections framework is the Hashtable and LinkedList implementation of the Map interface. It stores its entries in a Hashtable and uses a doubly-linked list internally to maintain the insertion order.
Ex: Java program to find FirstNonRepeatingCharacter. In the below example first non-repeating character is 't'.
Code Sample 1:
Code Sample 2:
in the code sample 1 I used LinkedHashMap which maintains insertion hence getting the expected result where as in code sample 2 we are not maintaining any insertion order hence it fails.