Filter Map in Java
In this post, we’ll illustrate how to filter a Map in Java.
There are several ways to filter a map in Java, depending on the criteria and the desired output. Here are some of the common methods:
1. Using a loop
We can filter a map by its keys or values using enhanced for loop. This method involves iterating over the map’s entry set and checking each key or value against a condition. The filtered entries can be stored in another map or collection. For example, to filter a map by its keys, we can use the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.HashMap; import java.util.Map; class Main { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); hashMap.put("BLACK", "#000000"); hashMap.put("GREEN", "#008000"); hashMap.put("BROWN", "#A52A2A"); Map<String, String> filteredMap = new HashMap<>(); // iterate through the map using entrySet() for (Map.Entry<String, String> entry: hashMap.entrySet()) { // filter keys that start with `B` if (entry.getKey().startsWith("B")) { filteredMap.put(entry.getKey(), entry.getValue()); } } // {BLUE=#0000FF, BLACK=#000000, BROWN=#A52A2A} System.out.println(filteredMap); } } |
2. Using Stream API
In Java 8 and above, the recommended approach to filter a map by its keys or values is using the Stream API. This method involves converting the map’s entry set into a stream and applying the filter() method with a predicate. Then collect the filtered stream into another map by using the Collectors.toMap() method by providing proper mapping methods for extracting keys and values from stream elements. This can be illustrated by this example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Map; import java.util.HashMap; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); hashMap.put("BLACK", "#000000"); hashMap.put("GREEN", "#008000"); hashMap.put("BROWN", "#A52A2A"); // Java 8 and above: Map<String, String> filteredMap = hashMap.entrySet() .stream() .filter(entry -> entry.getKey().startsWith("B")) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // {BLUE=#0000FF, BLACK=#000000, BROWN=#A52A2A} System.out.println(filteredMap); } } |
The filtered stream can also be collected into a string using the Collectors.joining() method. For example, the following code filters a map by its keys and get the string representation of the filtered map:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Map; import java.util.HashMap; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLUE", "#0000FF"); hashMap.put("BLACK", "#000000"); hashMap.put("GREEN", "#008000"); hashMap.put("BROWN", "#A52A2A"); String filteredMap = hashMap.entrySet() .stream() .filter(entry -> entry.getKey().startsWith("B")) .map(Map.Entry::toString) .collect(Collectors.joining(", ", "{", "}")); // {BLUE=#0000FF, BLACK=#000000, BROWN=#A52A2A} System.out.println(filteredMap); } } |
That’s all about filtering a Map in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)