java hashmap key iteration

前端 未结 7 679
谎友^
谎友^ 2020-12-29 06:46

Is there any way to iterate through a java Hashmap and print out all the values for every key that is a part of the Hashmap?

7条回答
  •  感情败类
    2020-12-29 07:09

    Yes, you do this by getting the entrySet() of the map. For example:

    Map map = new HashMap();
    
    // ...
    
    for (Map.Entry entry : map.entrySet()) {
        System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
    }
    

    (Ofcourse, replace String and Object with the types that your particular Map has - the code above is just an example).

提交回复
热议问题