java hashmap key iteration

前端 未结 7 651
谎友^
谎友^ 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:19

    With for-each loop, use Map.keySet() for iterating keys, Map.values() for iterating values and Map.entrySet() for iterating key/value pairs.

    Note that all these are direct views to the map that was used to acquire them so any modification you make to any of the three or the map itself will reflect to all the others too.

提交回复
热议问题