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.
StringBuilder
as a key instead ofString
? I do not suppose you are updating thekey
after insertion, so you only end up calling.toString()
every time you want to do something with the key.StringBuilder
in cases where a lot of appending happens, I was not saying you should useString
to replace it. I only suggested usingString
as the keys of your map by calling.toString()
onStringBuilder
when you put an entry in your map, instead of having to call.toString()
each time you retrieve an entry.