HashMap Searching For A Specific Value in Multiple Keys

后端 未结 3 1746
别跟我提以往
别跟我提以往 2021-01-29 10:34

I\'m checking to see if a key in my HashMap exists, if it does, I also want to check to see if any other keys have a value with the same name as that of the original key I check

3条回答
  •  灰色年华
    2021-01-29 11:05

    You will want to look at each entry in the HashMap. This loop should check the contents of the ArrayList for your searchcourse and print out the key that contained the value.

    for (Map.Entry entries : hashmap.entrySet()) {
        if (entries.getValue().contains(searchcourse)) {
            System.out.println(entries.getKey() + " contains " + searchcourse);
        }
    }
    

    Here are the relevant javadocs:

    Map.Entry

    HashMap entrySet method

    ArrayList contains method

提交回复
热议问题