I have this code:
HashMap<String, Integer> dupeMap = new HashMap<>();
for (String name : sortWords)
if(dupeMap.containsKey(name)) {
int count = dupeMap.get(name);
dupeMap.put(name, ++count);
} else {
dupeMap.put(name, 1);
}
System.out.printf("Duplicates: %s\n", dupeMap.entrySet());
I want it to print out duplicates after iterating through my ArrayList (it's been created beforehand and it just reads the given file and sorts the strings in alphabetic order). As of now it prints out duplicates this way:
Duplicates: [Eight=1, Two=2, Seven=2, Four=1]
etc. Is there a way to print it out in pairs (key=value\n key=value etc) and without actually mentioning the unique words?
I'm very new to Maps, so even for this piece of code I had to search stackoverflow, although I had the same idea in my mind, just couldn't come up with a good way to write it.