Java Map, filter with values properties
dictionary, filter, java
Solution
You can use filters from Guava and the `Predicate` interface.
Predicate<T> yourFilter = new Predicate<T>() {
public boolean apply(T o) {
// your filter
}
};
So, simple example would be:
Predicate<Integer> evenFilter = new Predicate<Integer>() {
public boolean apply(Integer i) {
return (i % 2 == 0);
}
};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Map<Integer, Integer> evenMap = Maps.filterValues(map, evenFilter);
Problem
I have a ``` TreeMap resMap new TreeMap<String, Map<String, String>>(); ``` I would like to filter and keep only entries that values contains a known pair, let's say ('mike' => 'jordan'), and avoid a loop like below Is there in my included libraries apache.commons and google.common a filter method (that probably would do a loop too, but at least it's less verbose ``` for (Entry<String, TreeMap<String, String>> el : resMap.entrySet()){ if (el.getValue().get("mike").equals("jordan")){ // } } ```