如何从 Java 中的 HashMap 获取关键位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28593441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:52:06  来源:igfitidea点击:

How to get key position from a HashMap in Java

java

提问by aNNgeL0

How can I get the key position in the map? How can I see on which position is "Audi" and "BMW"?

如何获取地图中的关键位置?我怎样才能看到“奥迪”和“宝马”在哪个位置?

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Audi", 3);
map.put("BMW", 5);

回答by Adam

As other answers state you need to use a structure like java.util.LinkedHashMap. LinkedHashMap maintains its keys internally using a LinkedEntrySet, this does not formally provide order, but iterates in the insertion order used.

正如其他答案所述,您需要使用像java.util.LinkedHashMap. LinkedHashMap 在内部使用 a 维护其键LinkedEntrySet,这并没有正式提供顺序,而是按照使用的插入顺序进行迭代。

If you pass the Map.keySet()into a List implementation you can make use of the List.indexOf(Object)method without having to write any of the extra code in the other answer.

如果将 传递Map.keySet()到 List 实现中,则可以使用该List.indexOf(Object)方法而无需在其他答案中编写任何额外代码。

Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("Audi", 3);
map.put("BMW", 5);
map.put("Vauxhall", 7);

List<String> indexes = new ArrayList<String>(map.keySet()); // <== Set to List

// BOOM !

System.out.println(indexes.indexOf("Audi"));     // ==> 0
System.out.println(indexes.indexOf("BMW"));      // ==> 1
System.out.println(indexes.indexOf("Vauxhall")); // ==> 2

回答by wassgren

You can't. From the HashMap JavaDocs:

你不能。来自HashMap JavaDocs

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Map接口的基于哈希表的实现。此实现提供了所有可选的映射操作,并允许空值和空键。(HashMap 类大致等同于 Hashtable,只是它是不同步的并且允许空值。)该类不保证映射的顺序;特别是,它不保证订单会随着时间的推移保持不变。

So, the order may vary between iterations. If you need to preserve the order you can take a look at LinkedHashMap

因此,迭代之间的顺序可能会有所不同。如果您需要保留订单,您可以查看LinkedHashMap

From the LinkedHashMap JavaDocs:

LinkedHashMap JavaDocs

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接口的哈希表和链表实现,迭代顺序可预测。此实现与 HashMap 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,这通常是键被插入到映射中的顺序(插入顺序)。

So, to find the key position you basically need to iterate the keys and count the position of the key you are searching for.

因此,要找到键位置,您基本上需要迭代键并计算要搜索的键的位置。

On a side note, IMO this may not be the best use of the Mapdatatype. I believe that if you really need the position you should use some type of List(e.g. ArrayList) that actually preserves the order and you can use the getmethod to retrieve elements for a certain index.

附带说明一下,IMO 这可能不是Map数据类型的最佳用途。我相信,如果您真的需要该位置,您应该使用某种类型的List(例如ArrayList)来实际保留顺序,并且您可以使用该get方法来检索某个索引的元素。

回答by mk.

You can't. The keys on a Map and a HashMap are not ordered. You'll need to use a structure that preserves order, such as a LinkedHashMap.

你不能。Map 和 HashMap 上的键没有排序。您需要使用保留顺序的结构,例如LinkedHashMap

Note that LinkedHashMap does not provide a method that gets keys by position, so this is only appropriate if you are going to be using an iterator.

请注意,LinkedHashMap 不提供按位置获取键的方法,因此这仅适用于您要使用迭代器的情况。

The alternative is to create a second Map that maps from your key to the Integer position, and add to it as you go along:

另一种方法是创建第二个 Map ,从您的键映射到 Integer 位置,并在进行时添加到它:

Map<String, Integer> indexMap = new HashMap<String, Integer>();
indexMap.put("Audi", 0);
indexMap.put("BMW", 1);

For a more elegant solution, you might need to give more information about what you're doing.

要获得更优雅的解决方案,您可能需要提供有关您正在执行的操作的更多信息。