Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find a key in a multiple-value HashMap?

my task is to make a synonym dictionary using HashMaps and Sets.
I have the following code in my main method:

public static void main(String[] args) {

        addSynonym("casa", "imobil");
        addSynonym("casa", "cladire");
        addSynonym("casa", "locuinta");
        addSynonym("casa", "camin");
        addSynonym("casa", "domiciuliu");

        addSynonym("jucarie", "joc");
        addSynonym("jucarie", "marioneta");
        addSynonym("jucarie", "papusa");
        addSynonym("jucarie", "pantin");

        addSynonym("om", "barbat");
        addSynonym("om", "persoana");

        afisarearray(getSynonyms("camin"));
    }

One Method to insert the synonyms and the keys:

static Map<String, ArrayList<String>> synonymTable = new HashMap<String, ArrayList<String>>();

    public static void addSynonym(String word, String synonym) {
        ArrayList<String> checklist = synonymTable.get(word);
        if (checklist == null) {
            ArrayList<String> temporarylist = new ArrayList<String>();
            temporarylist.add(synonym);

            synonymTable.put(word, temporarylist);
        } else {
            synonymTable.get(word).add(synonym);
        }
    }

One Method to display an ArrayList:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    public static void afisarearray(ArrayList<String> list) {
        if (list != null) {
            for (String s : list) {
                System.out.println(s + "\n");
            }
        } else {
            System.out.println("Empty list");
        }
    };

I want to find all the synonyms of a word for example the word "camin". And I wrote the following Method to do it, but it’s not working, it is returning an empty set of keys:

public static ArrayList<String> getSynonyms(String word) {
        if (word == "") {
            System.out.println("No word to serach for ");
            return null;
        } else {

            Set<String> keySet = new HashSet<String>();

            for (Map.Entry<String, ArrayList<String>> entry : synonymTable.entrySet()) {
                if (entry.getValue().equals(word)) {
                    keySet.add(entry.getKey());
                }
            }

            if (keySet.isEmpty()) {
                System.out.println("No keys found");
                return null;
            } else {

                ArrayList<String> newlist = new ArrayList<String>();

                for (String s : keySet) {
                    newlist.addAll(synonymTable.get(s));
                }
                return newlist;
            }
        }
    }

The problem is inside this if:

if (entry.getValue().equals(word))

is a comparison between an ArrayList and a string.
I don’t know how to correct it.

>Solution :

Try this, I also cleaned up your code a bit for readability

public static List<String> getSynonyms(String word) {
    if (word == null || word.trim().isEmpty()) {
        System.out.println("No word to serach for ");
        return null;
    }

    Set<String> keySet = new HashSet<String>();

    for (Map.Entry<String, List<String>> entry : synonymTable.entrySet()) {
        if (entry.getValue().contains(word)) {
            keySet.add(entry.getKey());             
        }
    }

    if (keySet.isEmpty()) {
        System.out.println("No keys found");
        return null;
    }
    
    //de-dupe and sort
    Collection<String> terms = new TreeSet<>();

    for (String s : keySet) {
        terms.addAll(synonymTable.get(s));
    }
    
    return new ArrayList<>(terms);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading