Java HashMap:如何按索引获取键和值?

时间:2021-11-09 19:14:59

I am trying to use a HashMap to map a unique string to a string ArrayList like this:

我正在尝试使用HashMap将一个唯一的字符串映射到一个字符串ArrayList,如下所示:

HashMap<String, ArrayList<String>>

Basically, I want to be able to access the keys by number, not by using the key's name. And I want to be able to access said key's value, to iterate over it. I'm imagining something like this:

基本上,我希望能够按数字访问密钥,而不是使用密钥的名称。我想要能够访问说键的值,来遍历它。我想象的是这样的:

for(all keys in my hashmap) {
    for(int i=0; i < myhashmap.currentKey.getValue.size(); i++) {
        // do things with the hashmaps elements
    }
}

Is there an easy way to do this?

有简单的方法吗?

8 个解决方案

#1


26  

You can iterate over keys by calling map.keySet(), or iterate over the entries by calling map.entrySet(). Iterating over entries will probably be faster.

您可以通过调用map.keySet()来迭代键,或者通过调用map.entrySet()来迭代条目。遍历条目可能会更快。

for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    List<String> list = entry.getValue();
    // Do things with the list
}

If you want to ensure that you iterate over the keys in the same order you inserted them then use a LinkedHashMap.

如果您想确保您在插入它们的同一顺序中遍历这些键,那么可以使用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.

顺便说一下,我建议将映射声明的类型改为 >。总是最好根据接口而不是实现来声明类型。 ,>

#2


53  

Here is the general solution if you really only want the first key's value

如果你只想要第一个键的值,这就是通解

Object firstKey = myHashMap.keySet().toArray()[0];
Object valueForFirstKey = myHashMap.get(firstKey);

#3


7  

HashMaps are not ordered, unless you use a LinkedHashMap or SortedMap. In this case, you may want a LinkedHashMap. This will iterate in order of insertion (or in order of last access if you prefer). In this case, it would be

HashMaps不是有序的,除非您使用LinkedHashMap或SortedMap。在这种情况下,您可能需要一个LinkedHashMap。这将按照插入顺序进行迭代(如果您喜欢的话,可以按最后访问的顺序)。在这种情况下,它是。

int index = 0;
for ( Map.Entry<String,ArrayList<String>> e : myHashMap.iterator().entrySet() ) {
    String key = e.getKey();
    ArrayList<String> val = e.getValue();
    index++;
}

There 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.

映射中没有直接的get(index),因为它是键/值对的无序列表。LinkedHashMap是一个特殊的情况,可以保持顺序。

#4


2  

for (Object key : data.keySet()) {
    String lKey = (String) key;
    List<String> list = data.get(key);
}

#5


2  

A solution is already selected. However, I post this solution for those who want to use an alternative approach:

已经选择了一个解决方案。然而,我将这个解决方案发布给那些想要使用另一种方法的人:

// use LinkedHashMap if you want to read values from the hashmap in the same order as you put them into it
private ArrayList<String> getMapValueAt(LinkedHashMap<String, ArrayList<String>> hashMap, int index)
{
    Map.Entry<String, ArrayList<String>> entry = (Map.Entry<String, ArrayList<String>>) hashMap.entrySet().toArray()[index];
    return entry.getValue();
}

#6


1  

You can do:

你能做什么:

for(String key: hashMap.keySet()){
    for(String value: hashMap.get(key)) {
        // use the value here
    }
}

This will iterate over every key, and then every value of the list associated with each key.

这将遍历每个键,然后遍历与每个键关联的列表的每个值。

#7


0  

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:

HashMaps不会按照特定的顺序保存键/值对。它们是基于每个键从其Object.hashCode()方法返回的散列进行排序的。不过,您可以使用以下迭代器对键/值对集进行迭代:

for (String key : hashmap.keySet()) 
{
    for (list : hashmap.get(key))
    {
        //list.toString()
    }
}

#8


0  

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() method

如果您不关心实际的键,可以使用它的values()方法来迭代映射的所有值

Map<String, List<String>> myMap;

for ( List<String> stringList : myMap.values() ) {
    for ( String myString : stringList ) {
        // process the string here
    }
}

The values() method is part of the Map interface and returns a Collection view of the values in the map.

values()方法是Map接口的一部分,它返回Map中值的集合视图。

#1


26  

You can iterate over keys by calling map.keySet(), or iterate over the entries by calling map.entrySet(). Iterating over entries will probably be faster.

您可以通过调用map.keySet()来迭代键,或者通过调用map.entrySet()来迭代条目。遍历条目可能会更快。

for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    List<String> list = entry.getValue();
    // Do things with the list
}

If you want to ensure that you iterate over the keys in the same order you inserted them then use a LinkedHashMap.

如果您想确保您在插入它们的同一顺序中遍历这些键,那么可以使用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.

顺便说一下,我建议将映射声明的类型改为 >。总是最好根据接口而不是实现来声明类型。 ,>

#2


53  

Here is the general solution if you really only want the first key's value

如果你只想要第一个键的值,这就是通解

Object firstKey = myHashMap.keySet().toArray()[0];
Object valueForFirstKey = myHashMap.get(firstKey);

#3


7  

HashMaps are not ordered, unless you use a LinkedHashMap or SortedMap. In this case, you may want a LinkedHashMap. This will iterate in order of insertion (or in order of last access if you prefer). In this case, it would be

HashMaps不是有序的,除非您使用LinkedHashMap或SortedMap。在这种情况下,您可能需要一个LinkedHashMap。这将按照插入顺序进行迭代(如果您喜欢的话,可以按最后访问的顺序)。在这种情况下,它是。

int index = 0;
for ( Map.Entry<String,ArrayList<String>> e : myHashMap.iterator().entrySet() ) {
    String key = e.getKey();
    ArrayList<String> val = e.getValue();
    index++;
}

There 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.

映射中没有直接的get(index),因为它是键/值对的无序列表。LinkedHashMap是一个特殊的情况,可以保持顺序。

#4


2  

for (Object key : data.keySet()) {
    String lKey = (String) key;
    List<String> list = data.get(key);
}

#5


2  

A solution is already selected. However, I post this solution for those who want to use an alternative approach:

已经选择了一个解决方案。然而,我将这个解决方案发布给那些想要使用另一种方法的人:

// use LinkedHashMap if you want to read values from the hashmap in the same order as you put them into it
private ArrayList<String> getMapValueAt(LinkedHashMap<String, ArrayList<String>> hashMap, int index)
{
    Map.Entry<String, ArrayList<String>> entry = (Map.Entry<String, ArrayList<String>>) hashMap.entrySet().toArray()[index];
    return entry.getValue();
}

#6


1  

You can do:

你能做什么:

for(String key: hashMap.keySet()){
    for(String value: hashMap.get(key)) {
        // use the value here
    }
}

This will iterate over every key, and then every value of the list associated with each key.

这将遍历每个键,然后遍历与每个键关联的列表的每个值。

#7


0  

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:

HashMaps不会按照特定的顺序保存键/值对。它们是基于每个键从其Object.hashCode()方法返回的散列进行排序的。不过,您可以使用以下迭代器对键/值对集进行迭代:

for (String key : hashmap.keySet()) 
{
    for (list : hashmap.get(key))
    {
        //list.toString()
    }
}

#8


0  

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() method

如果您不关心实际的键,可以使用它的values()方法来迭代映射的所有值

Map<String, List<String>> myMap;

for ( List<String> stringList : myMap.values() ) {
    for ( String myString : stringList ) {
        // process the string here
    }
}

The values() method is part of the Map interface and returns a Collection view of the values in the map.

values()方法是Map接口的一部分,它返回Map中值的集合视图。