遍历HashMap的最常用四种方法记记记

1.hashmap.keySet(),此法可得key和value;
2.Map.Entry<x,x> entry ; foreach遍历 hashmap.entrySet();
3.对第2种创建遍历器 Iterator <Map.Entry<x, x>> it , 遍历it.hasNext();
4.hashmap.values() 不好用,只能得value。

public static void main(String[] args) {
    
    
        HashMap<Integer, String> hashmap = new HashMap<>();
        hashmap.put(1,"gogo");
        hashmap.put(2,"wade");
        hashmap.put(3,"james");
        hashmap.put(4,"curry");
        // 1. 通过Map.keySet遍历key和value:
        for (int key : hashmap.keySet()){
    
    
            System.out.println("key: "+ key + "; value: " + hashmap.get(key));
        }
 
        //2. 通过Map.entrySet使用iterator遍历key和value:
        Iterator<Map.Entry<Integer, String>> it = hashmap.entrySet().iterator();
        while (it.hasNext()){
    
    
            Map.Entry<Integer, String> entry = it.next();
            System.out.println("key: "+ entry.getKey() + "; value: " + entry.getValue());
        }
 
        //3. 通过Map.entrySet遍历key和value
        for(Map.Entry<Integer, String> entry : hashmap.entrySet()){
    
    
            System.out.println("key: "+ entry.getKey() + "; value: " + entry.getValue());
        }
 
        //4. 通过Map.values()遍历所有的value,但不能遍历key
        for (String value : hashmap.values()) {
    
    
            System.out.println("value: "+value);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_32301683/article/details/108503213
今日推荐