20

我想存储值并从 Java HashMap 中检索它们。

这是我到目前为止所拥有的:

public void processHashMap()
{
    HashMap hm = new HashMap();
    hm.put(1,"godric gryfindor");
    hm.put(2,"helga hufflepuff"); 
    hm.put(3,"rowena ravenclaw");
    hm.put(4,"salazaar slytherin");
}

我想从 HashMap 中检索所有键和值作为 Java 集合或实用程序集(例如LinkedList)。

我知道如果我知道密钥,我可以获得价值,如下所示:

hm.get(1);

有没有办法将键值作为列表检索?

4

7 回答 7

37

我使用这三种方法来迭代地图。所有方法(keySet, values, entrySet)都返回一个集合。

// Given the following map
Map<KeyClass, ValueClass> myMap;

// Iterate all keys
for (KeyClass key  : myMap.keySet()) 
    System.out.println(key);

// Iterate all values
for (ValueClass value  : myMap.values()) 
    System.out.println(value);

// Iterate all key/value pairs
for (Entry<KeyClass, ValueClass> entry  : myMap.entrySet()) 
    System.out.println(entry.getKey() + " - " + entry.getValue());

从 Java 8 开始,我经常将Streamslambda 表达式一起使用。

    // Iterate all keys
    myMap.keySet().stream().forEach(key -> System.out.println(key));

    // Iterate all values
    myMap.values().parallelStream().forEach(value -> System.out.println(value));

    // Iterate all key/value pairs
    myMap.entrySet().stream().forEach(entry -> System.out.println(entry.getKey() + " - " + entry.getValue()));
于 2012-06-15T10:29:36.507 回答
15

Java Hashmap 键值示例:

public void processHashMap() {
    //add keys->value pairs to a hashmap:
    HashMap hm = new HashMap();
    hm.put(1, "godric gryfindor");
    hm.put(2, "helga hufflepuff");
    hm.put(3, "rowena ravenclaw");
    hm.put(4, "salazaar slytherin");

    //Then get data back out of it:
    LinkedList ll = new LinkedList();
    Iterator itr = hm.keySet().iterator();
    while(itr.hasNext()) {
        String key = itr.next();
        ll.add(key);
    }

    System.out.print(ll);  //The key list will be printed.
}
于 2012-06-23T19:15:31.810 回答
10

map.keySet()会给你所有的钥匙

于 2012-06-15T10:19:53.733 回答
3
//import statements
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;

// hashmap test class
public class HashMapTest {

    public static void main(String args[]) {

        HashMap<Integer,String> hashMap = new HashMap<Integer,String>(); 

        hashMap.put(91, "India");
        hashMap.put(34, "Spain");
        hashMap.put(63, "Philippines");
        hashMap.put(41, "Switzerland");

        // sorting elements
        System.out.println("Unsorted HashMap: " + hashMap);
        TreeMap<Integer,String> sortedHashMap = new TreeMap<Integer,String>(hashMap);
        System.out.println("Sorted HashMap: " + sortedHashMap);

        // hashmap empty check
        boolean isHashMapEmpty = hashMap.isEmpty();
        System.out.println("HashMap Empty: " + isHashMapEmpty);

        // hashmap size
        System.out.println("HashMap Size: " + hashMap.size());

        // hashmap iteration and printing
        Iterator<Integer> keyIterator = hashMap.keySet().iterator();
        while(keyIterator.hasNext()) {
            Integer key = keyIterator.next();
            System.out.println("Code=" + key + "  Country=" + hashMap.get(key));
        }

        // searching element by key and value
        System.out.println("Does HashMap contains 91 as key: " + hashMap.containsKey(91));
        System.out.println("Does HashMap contains India as value: " + hashMap.containsValue("India"));

        // deleting element by key
        Integer key = 91;
        Object value = hashMap.remove(key);
        System.out.println("Following item is removed from HashMap: " + value);

    }

}
于 2014-02-21T22:27:19.317 回答
1

您可以使用它keySet()来检索密钥。您还应该考虑在地图中添加输入,例如:

Map<Integer, String> hm = new HashMap<Integer, String>();
hm.put(1,"godric gryfindor");
hm.put(2,"helga hufflepuff"); 
hm.put(3,"rowena ravenclaw");
hm.put(4,"salazaar slytherin");

Set<Integer> keys = hm.keySet();
于 2012-06-15T10:22:09.663 回答
1
public static void main(String[] args) {

    HashMap<String, String> hashmap = new HashMap<String, String>();

    hashmap.put("one", "1");
    hashmap.put("two", "2");
    hashmap.put("three", "3");
    hashmap.put("four", "4");
    hashmap.put("five", "5");
    hashmap.put("six", "6");

    Iterator<String> keyIterator = hashmap.keySet().iterator();
    Iterator<String> valueIterator = hashmap.values().iterator();

    while (keyIterator.hasNext()) {
        System.out.println("key: " + keyIterator.next());
    }

    while (valueIterator.hasNext()) {
        System.out.println("value: " + valueIterator.next());
    }
}
于 2012-06-15T10:26:56.223 回答
0
void hashMapExample(){

    HashMap<String, String> hMap = new HashMap<String, String>();
    hMap.put("key1", "val1");
    hMap.put("key2", "val2");
    hMap.put("key3", "val3");
    hMap.put("key4", "val4");
    hMap.put("key5", "val5");

    if(hMap != null && !hMap.isEmpty()){
        for(String key : hMap.keySet()){
            System.out.println(key+":"+hMap.get(key));
        }
    }
}
于 2016-06-08T06:35:26.280 回答