Learn to filter a Map by keys or values using forEach() loop and Stream.filter() API in Java 8.
1. Setup
For the code examples, we will use the following Map of users. Map keys are Integer types, and Map values are User instances.
Map<Integer, User> usersMap = Map.of(
1, new User(1, "Alex"),
2, new User(2, "Allen"),
3, new User(3, "Brian"),
4, new User(4, "Bob"),
5, new User(5, "Charles"),
6, new User(6, "David"),
8, new User(7, "Don"),
9, new User(8, "Dave"));
We will filter the Map against the following List of keys. The List can contain anything. We are storing user ids to keep the examples easy to understand.
List<Integer> idList = List.of(1,3,5,7);
2. Filter a Map by List of Keys
Suppose we have a List of ids of users and we want to get the submap consisting of the users whose id is present in the List.
To do so, we will iterate over the Map‘s EntrySet. Then we will check for the id to be present in the List in the filter() function. Finally, we will collect the matching entry sets in a new Map using Collectors.toUnmodifiableMap().
Map<Integer, User> filteredMap = usersMap.entrySet()
.stream()
.filter(entry -> idList.contains(entry.getKey()))
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(filteredMap);
The program output.
{1=User(id=1, name=Alex), 3=User(id=3, name=Brian), 5=User(id=5, name=Charles), 7=User(id=7, name=Don)}
If we want to change the evaluation criteria then we need to change the expression in the filter() function. For example, if we want to check for the User’s id into the List then change the filter() expression to as follows:
...
.filter(entry -> idList.contains(entry.getValue().getId()))
...
3. Filter a Map and Collect Values into a List
Another usecase can be when we want to filter the Map and collect values into a List for entrysets whose key is present in the List.
List<User> usersList = usersMap.values()
.stream()
.filter(user -> idList.contains(user.getId()))
.collect(Collectors.toUnmodifiableList());
System.out.println(usersList);
The program output.
[User(id=1, name=Alex), User(id=3, name=Brian), User(id=5, name=Charles), User(id=7, name=Don)]
4. Filter a Map using forEach()
We can also filter a Map using forEach() loop. The forEach loop takes a Consumer action that accepts a single input argument and returns no result.
In our case, we will pass the Map.Entry to the Consumer and match the key or value from the List item. If the Entry matches the condition, we will add the entry.getValue() to a new List.
List<User> usersList = new ArrayList<>();
usersMap.entrySet().forEach( entry -> {
if(idList.contains(entry.getValue().getId())) {
usersList.add(entry.getValue());
}
});
System.out.println(usersList);
The program output.
[User(id=1, name=Alex), User(id=3, name=Brian), User(id=5, name=Charles), User(id=7, name=Don)]
5. Conclusion
In this Java tutorial, we learned to filter a HashMap by keys or values and collect the matching Entry instances into a submap. We also learned to collect the values in a List after filtering the Map keys against a List using Stream.filter() and forEach() APIs.
Happy Learning !!