Home > OS >  How to find a key in a multiple-value HashMap?
How to find a key in a multiple-value HashMap?

Time:04-29

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:

    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.

CodePudding user response:

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);
}
  • Related