Print all key/value pairs in a Java ConcurrentHashMap

前端 未结 6 787
清歌不尽
清歌不尽 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:56

    The HashMap has forEach as part of its structure. You can use that with a lambda expression to print out the contents in a one liner such as:

    map.forEach((k,v)-> System.out.println(k+", "+v));
    
    or
    
    map.forEach((k,v)-> System.out.println("key: "+k+", value: "+v));
    

提交回复
热议问题