user8439161
user8439161

Reputation:

Finding Duplicates in the Value of HashMap

private HashMap <Integer, String> ID_TAGS;
    private HashMap <String, Integer> TAGS_ID;
    private HashMap <String, String> TAGS_TRANSLATIONS;
    private final ArrayList <Integer> INCLUSIONLIST;
    private final ArrayList <Integer> EXCLUSIONLIST;



public DuplicationFinder(HashMap <Integer, String> id_tags, HashMap <String, String> tags_translations, ArrayList <Integer> exclusionList, ArrayList <Integer> inclusionList) {
    this.ID_TAGS = id_tags;
    this.TAGS_TRANSLATIONS = tags_translations;
    this.INCLUSIONLIST = inclusionList;
    this.EXCLUSIONLIST = exclusionList;
    TAGS_ID = new HashMap <>();
    for(Entry <Integer, String> e : ID_TAGS.entrySet()){
        TAGS_ID.put(e.getValue(), e.getKey());
    }
}
/**
 * Findet die Duplikate und gibt die ID's zurück.
 * @return
 */
public Set <Integer> findDuplicates(){
    Set <Integer> duplicates = new LinkedHashSet <>();
    for(Entry <Integer, String> e : ID_TAGS.entrySet()) {
        HashMap <String, String> cloneWithoutTag= new HashMap <>(TAGS_TRANSLATIONS);
        int id = e.getKey();
        String tag = e.getValue();
        cloneWithoutTag.remove(tag);
        if(cloneWithoutTag.containsValue(TAGS_TRANSLATIONS.get(tag))) {
            duplicates.add(id);
        }
    }
    duplicates.addAll(EXCLUSIONLIST);
    duplicates.removeAll(INCLUSIONLIST);
    Iterator<Integer> nextD = duplicates.iterator();
    while(nextD.hasNext()) {
        System.out.println(lookUp(ID_TAGS.get(nextD.next())));
    }
    return duplicates;
}

public String lookUp(String tag) {
    return TAGS_TRANSLATIONS.get(tag);
}

public int getID(String tag) {
    return TAGS_ID.get(tag);
}

}

I don't know if anybody can help me with that. I'll try to find some Keys with the same Value in the TAGS_TRANSLATIONS-HashMap. My thought was that when the chosen key is not int the clone of the map you can look if the same value is still in there. It's working so far but i have a Problem, some Values like "Meeting" are only one time in there and also got in the Output. Now i'll try to find the Error. Thanks in advance for help :)

Upvotes: 1

Views: 5385

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521103

Let's assume you have the following map:

Map<Tags, Translation> someMap;

You could get all the values as a collection, which would include duplicates, and then use Collections#frequency() to find the frequency of each item. If the frequency be greater than one, then the translation is a duplicate.

Collection<Translation> translations = someMap.values();
Set<Translation> dupeSet = new HashSet<>();

for (Translation t : translations) {
    if (Collections.frequency(translations, t) > 1) {
        dupeSet.add(t);
    }
}

Note that this code will touch each duplicate translation, but since we are storing the duplicates in a set, a given duplicate translation should appear only once in the final result.

Upvotes: 3

Related Questions