Print all key/value pairs in a Java ConcurrentHashMap

137,762

Solution 1

I tested your code and works properly. I've added a small demo with another way to print all the data in the map:

ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);

for (String key : map.keySet()) {
    System.out.println(key + " " + map.get(key));
}

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    String key = entry.getKey().toString();
    Integer value = entry.getValue();
    System.out.println("key, " + key + " value " + value);
}

Solution 2

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));

Solution 3

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.

Solution 4

  //best and simple way to show keys and values

    //initialize map
    Map<Integer, String> map = new HashMap<Integer, String>();

   //Add some values
    map.put(1, "Hi");
    map.put(2, "Hello");

    // iterate map using entryset in for loop
    for(Entry<Integer, String> entry : map.entrySet())
    {   //print keys and values
         System.out.println(entry.getKey() + " : " +entry.getValue());
    }

   //Result : 
    1 : Hi
    2 : Hello
Share:
137,762
DanGordon
Author by

DanGordon

Updated on April 24, 2020

Comments

  • DanGordon
    DanGordon about 4 years

    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 about the buckets/hashcode. Actually to be honest the output it quite strange, its possible my program is incorrect, but I first want to make sure this part is what I want to be using.

    for (Entry<StringBuilder, Integer> entry : wordCountMap.entrySet()) {
        String key = entry.getKey().toString();
        Integer value = entry.getValue();
        System.out.println("key, " + key + " value " + value);
    }
    

    This gives output for about 10 different keys, with counts that seem to be the sum of the number of total inserts into the map.

  • Daniël Knippers
    Daniël Knippers about 10 years
    To be fair, he already has the loop you give as an answer (using entrySet(), getKey(), getValue()). You just squeeze it on 1 line :)
  • DanGordon
    DanGordon about 10 years
    Does it matter that I am using StringBuilder?
  • DanGordon
    DanGordon about 10 years
    I've changed my map to <Integer [], Integer>. When I do println, I get something like this back: @2d51e135 How do I get the actual value?
  • DanGordon
    DanGordon about 6 years
    If you are using Java 8, I suggest using Rick Il Grande's answer below. It is concise and utilizes the forEach available to collections in Java 8.