Print all key/value pairs in a Java ConcurrentHashMap

前端 未结 6 721
清歌不尽
清歌不尽 2021-02-02 17:40

I am trying to simply print all key/value pair(s) in a ConcurrentHashMap.

I found this code online that I thought would do it, but it seems to be getting information ab

6条回答
  •  孤城傲影
    2021-02-02 17:52

    You can do something like

    Iterator iterator = map.keySet().iterator();
    
    while (iterator.hasNext()) {
       String key = iterator.next().toString();
       Integer value = map.get(key);
    
       System.out.println(key + " " + value);
    }
    

    Here 'map' is your concurrent HashMap.

提交回复
热议问题