3

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.

2
  • 2
    You mean you want to remove the entries that have a count of 1? Commented May 4, 2017 at 21:47
  • Yeah, this and I want to print out the key-value pairs as regular strings Commented May 4, 2017 at 21:51

2 Answers 2

5

Filter the entry set to remove elements with a value of 1:

List<Map.Entry<String, Integer>> dupes = dupeMap.entrySet()
    .stream()
    .filter(e -> e.getValue() > 1)
    .collect(Collectors.toList());

Then:

System.out.printf("Duplicates: %s\n", dupes);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That works just fine. However, the code is too advanced for me, could you please explain what line 3 does?
@Kirill do you mean .filter(e -> e.getValue() > 1)? That just says that only entries whose value is greater than 1 (i.e. words whose count is greater than 1) should be retained from the stream.
4

You can also use removeIf() (added in JDK1.8) on the entrySet() to remove the words occured only once as shown below:

Set<Map.Entry<String, Integer>> elements = dupeMap.entrySet();
elements.removeIf(element->element.getValue() ==1);//removes words ocured only once
System.out.printf("Duplicates: %s\n",elements);

OUTPUT:

Duplicates: [Two=2, Seven=2]

If you are using JDK1.7 or earlier, you need to use Iterator to safely remove the words occured only once as shown below:

Set<Map.Entry<String, Integer>> elements = dupeMap.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = elements.iterator();
while(iterator.hasNext()) {
   Map.Entry<String, Integer> element = iterator.next();
   if(element.getValue() ==1) {
       iterator.remove();
    }
}
System.out.printf("Duplicatesss: %s\n",elements);

2 Comments

Thanks, I might actually even use JDK1.7 code, since a friend of mine suggested me to write this code as if JDK1.8 wasn't there (to better learn the basics, I suppose).
Yes, it is important to understand the concept of Iterator as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.