Java find all the key with specific value -


this question has answer here:

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

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -