Following the same principles found in filtering a map by keys and filtering a map by values, this example will show how to filter a map by entries while using java, java 8 and guava. Each of the snippets below will use a predefined map of months where the key is the month of year and the value represents the month spelled out.
Setup
Straight up Java
The core jdk lack the mechanics for filtering a map by Map.entry until java 8. As shown in the snippet below, the only effective way is to iterate over the Map.entrySet using a java 5 for loop and then applying criteria in an if statement, in this case checking if entry.value is a length of four.
Output
Java 8
The snippet below will show how to filter a map by entries using java 8. Using the powerful streams api, we will call entrySet.stream and filter months with a length of 4. We created this java.util.function.predicate by using a lambda expression. Then using Collectors.map we will map each element in the stream back into a map.
Output
Google Guava
Using guava Maps collection utility we will call Maps.filterEntries which accepts a map and a guava predicate as parameters. It will then filter entries that satisfy the predicate and return a map of the elements.