Java - using Map how to find matching Key when Values are stored a List of strings

前端 未结 4 787
萌比男神i
萌比男神i 2021-01-24 01:10

All,

I have a map with categories and subcategories as lists like this:

    Map> cat = new HashMap

        
4条回答
  •  面向向阳花
    2021-01-24 01:45

    You have to search for the value in the entire map:

    for (Entry> entry : cat.entrySet()) {
        for (String s : entry.getValue()) {
            if (s.equals("Carrot"))
                System.out.println(entry.getKey());
        }
    }
    

提交回复
热议问题