Java HashMap:如何通过索引获取键和值?
我正在尝试使用 HashMap 将唯一字符串映射到字符串 ArrayList,如下所示:
HashMap<String, ArrayList<String>>
基本上,我希望能够按数字访问键,而不是使用键的名称。我希望能够访问该键的值,并对其进行迭代。我正在想象这样的事情:
for(all keys in my hashmap) {
for(int i=0; i < myhashmap.currentKey.getValue.size(); i++) {
// do things with the hashmaps elements
}
}
有没有一种简单的方法可以做到这一点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您确实只想要第一个键的值,那么这是通用解决方案
Here is the general solution if you really only want the first key's value
您可以通过调用
map.keySet()
来迭代键,或者通过调用map.entrySet()
来迭代条目。迭代条目可能会更快。如果您想确保按照插入键的相同顺序迭代键,请使用
LinkedHashMap
。顺便说一句,我建议将映射的声明类型更改为
>
。最好总是根据接口而不是实现来声明类型。You can iterate over keys by calling
map.keySet()
, or iterate over the entries by callingmap.entrySet()
. Iterating over entries will probably be faster.If you want to ensure that you iterate over the keys in the same order you inserted them then use a
LinkedHashMap
.By the way, I'd recommend changing the declared type of the map to
<String, List<String>>
. Always best to declare types in terms of the interface rather than the implementation.HashMap 不是有序的,除非您使用
LinkedHashMap
或SortedMap
。在这种情况下,您可能需要一个LinkedHashMap
。这将按插入顺序进行迭代(如果您愿意,也可以按上次访问的顺序进行迭代)。在这种情况下,映射中没有直接的 get(index),因为它是键/值对的无序列表。
LinkedHashMap
是一种保持顺序的特殊情况。HashMaps are not ordered, unless you use a
LinkedHashMap
orSortedMap
. In this case, you may want aLinkedHashMap
. This will iterate in order of insertion (or in order of last access if you prefer). In this case, it would beThere is no direct get(index) in a map because it is an unordered list of key/value pairs.
LinkedHashMap
is a special case that keeps the order.Kotlin HashMap 解答
您可以通过索引获取键。然后通过key获取value。
Kotlin HashMap Answer
You can get key by index. Then get value by key.
您可以这样做:
这将迭代每个键,然后迭代与每个键关联的列表中的每个值。
You can do:
This will iterate over every key, and then every value of the list associated with each key.
已经选择了解决方案。但是,我为那些想要使用替代方法的人发布此解决方案:
A solution is already selected. However, I post this solution for those who want to use an alternative approach:
我遇到了同样的问题,阅读了不同相关问题的几个答案,并提出了我自己的课程。
示例(为了清楚起见,类型与 OP 问题不同):
请记住,它不会覆盖任何复杂方法,因此如果您想可靠地访问其中之一,则需要自己执行此操作。
编辑:我对 putAll() 方法进行了更改,因为旧方法很少有机会导致 HashMap 和 LinkedList 处于不同的状态。
I came across the same problem, read a couple of answers from different related questions and came up with my own class.
Example (types are differing from OPs question just for clarity):
Keep in mind that it does not override any of the complex methods, so you will need to do this on your own if you want to reliably access one of these.
Edit: I made a change to the putAll() method, because the old one had a rare chance to cause HashMap and LinkedList being in different states.
HashMap 不会按特定顺序保存键/值对。它们根据每个键从其 Object.hashCode() 方法返回的哈希值进行排序。但是,您可以使用迭代器来迭代键/值对集:
HashMaps don't keep your key/value pairs in a specific order. They are ordered based on the hash that each key's returns from its Object.hashCode() method. You can however iterate over the set of key/value pairs using an iterator with:
如果您不关心实际的键,迭代 Map 的所有值的一种简洁方法是使用其
values()
方法values( )
方法是 Map 接口的一部分,返回地图中值的 Collection 视图。If you don't care about the actual key, a concise way to iterate over all the Map's values would be to use its
values()
methodThe
values()
method is part of the Map interface and returns a Collection view of the values in the map.例如,您需要像这样创建多个 HashMap
You'll need to create multiple HashMaps like this for example
您可以使用 Kotlin 扩展函数
You can use Kotlin extension function
试试这个:
或者如果你想按索引
Try this:
or if you want by index