Why doesn't this work? In my understanding I should remove all of the double occurent names that means if "Willi" occurs 2 times in the map i remove both of them by calling elementNachWertAusMapEntfernen which does that for me. So i only have to find out which of the entries do have double occurencies which my code should solve. But the 4th condition is still false?!
package de.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/*
Wir brauchen keine Wiederholungen
*/
public class Solution {
public static HashMap<String, String> mapErstellen() {
HashMap<String,String> map = new HashMap<String,String>();
map.put("Ehrmann", "Willi");
map.put("Bauer2", "Willi");
map.put("Bauer3", "Klaus");
map.put("Kinski", "Klaus");
map.put("Müller", "Peter");
map.put("Bauer4", "Peter2");
map.put("Gundel", "Markus");
map.put("Bauer5", "Markus");
map.put("Meyers", "Steffan");
map.put("Bauer6", "Steffan");
return map;
}
public static void doppelteVornamenEntfernen(Map<String, String> map) {
Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
HashSet<String> set = new HashSet<String>();
while (iter.hasNext()) {
Map.Entry<String, String> next = iter.next();
String wert = next.getValue();
if (!set.add(wert)) {
elementNachWertAusMapEntfernen(map, wert);
}
}
}
public static void elementNachWertAusMapEntfernen(Map<String, String> map, String wert) {
HashMap<String, String> kopie = new HashMap<String, String>(map);
for (Map.Entry<String, String> paar : kopie.entrySet()) {
if (paar.getValue().equals(wert))
map.remove(paar.getKey());
}
}
public static void main(String[] args) {
}
}