首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java HashMap键值的存储和检索

Java HashMap键值的存储和检索
EN

Stack Overflow用户
提问于 2012-06-15 18:17:43
回答 6查看 101.4K关注 0票数 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");
}

我想以Java Collection或实用程序集(例如LinkedList)的形式从HashMap中检索所有键和值。

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

hm.get(1);

有没有办法以列表的形式检索键值?

EN

回答 6

Stack Overflow用户

发布于 2012-06-15 18:29:37

我使用这三种方法来迭代映射。所有方法(keySetvaluesentrySet)都返回一个集合。

// 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());

从Java8开始,我经常在lambda expressions中使用Streams

    // 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()));
票数 37
EN

Stack Overflow用户

发布于 2012-06-15 18:19:54

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

票数 10
EN

Stack Overflow用户

发布于 2014-02-22 06:27:19

//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);

    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11048783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档