Java find all the key with specific value -
this question has answer here:
- java hashmap: how key value? 29 answers
is there way find out key having same value in map. like
map.put("a","abc"); map.put("b","abc"); map.put("c","abc"); map.put("d","bcd");
here want find out key having value "abc".
you use guava filter.
map<string, string> map = maps.newhashmap(); map.put("a", "abc"); map.put("b", "abc"); map.put("c", "a3c"); map.put("d", "abc"); final string str = "a3c"; map<string, string> filteredmap = maps.filterentries(map, new predicate<map.entry<string, string>>() { @override public boolean apply(final map.entry<string, string> stringstringentry) { return stringstringentry.getvalue().equals(str); } });
this return map of map entries have str
value.
fyi provided function definition show doing suggest using predefined predicates eg:
map<string, string> filteredmap = maps.filtervalues(map, predicates.equalto(str));
Comments
Post a Comment