How to Filter a Map in Java 8 | Filter a Map in Java | Online Tutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to filter a Map in Java 8. We have been filtering the map in Java since over the years. But in previous versions of Java, if we want to filter a Map, we should loop the map and put a condition inside the loop based on the requirement.
Map<Integer,String> mobiles = new HashMap<>();
        mobiles.put(1, "iPhone 7");
        mobiles.put(2, "iPhone 6S");
        mobiles.put(3, "Samsung");
        mobiles.put(4, "1+");
        for (Map.Entry<Integer,String> mobis : mobiles.entrySet()) {
            if(mobis.getValue().equals("Samsung")){
                System.out.println("Filtered Value : "+mobis.getValue());
            }
        }
On the above, we have filtered a samsung mobile among 4.
Now we will see how do we do filter a Map in Java 8 using Stream API.
We can filter a Map in Java 8 by converting the map.entrySet() into Stream and followed by filter() method and then finally collect it using collect() method.
String result = mobiles.entrySet().stream() // converting into Stream
               .filter(map -> "Samsung".equals(map.getValue())) // Applying filter
               .map(map -> map.getValue()) // apply mapping
               .collect(Collectors.joining()); // collecting the data


Complete Example:
package com.onlinetutorialspoint.java8;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8_FilterMap {
    public static void main(String[] args) {
        Map<Integer,String> mobiles = new HashMap<>();
        mobiles.put(1, "iPhone 7");
        mobiles.put(2, "iPhone 6S");
        mobiles.put(3, "Samsung");
        mobiles.put(4, "1+");
        for (Map.Entry<Integer,String> mobis : mobiles.entrySet()) {
            if(mobis.getValue().equals("Samsung")){
                System.out.println("Filtered Value : "+mobis.getValue());
            }
        }
        // Java8 Filtering
        
        String result = mobiles.entrySet().stream()
                .filter(map -> "Samsung".equals(map.getValue()))
                .map(map -> map.getValue())
                .collect(Collectors.joining());

        System.out.println("Filtering With Value " + result);
        
        
        Map<Integer, String> deptMap2 = mobiles.entrySet().stream()
                .filter(map -> map.getKey() == 2)
                .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
        
        System.out.println("Filtering With Key : " + deptMap2);
    }
}
Output:
Java8_FilterMap
Filtered With Value Samsung
Filtered With Key : {2=iPhone 6S}
Happy Learning 🙂

Java 8 Read File Line By Line Example
How to get Stream count in Java 8
How to calculate Employees Salaries Java 8 summingInt
Java 8 foreach Example Tutorials
How to Merge Streams in Java 8
Java 8 groupingBy Example
How to sort a Map using Java8
Java 8 How to get common elements from two lists
Java 8 Stream Filter Example with Objects
How to Filter null values from Java8 Stream
User defined sorting with Java 8 Comparator
Hibernate Filter Example Xml Configuration
AngularJs lowercase Filter Example
AngularJs Currency Filter Example
AngularJs Orderby Filter Example

Java 8 Read File Line By Line Example
How to get Stream count in Java 8
How to calculate Employees Salaries Java 8 summingInt
Java 8 foreach Example Tutorials
How to Merge Streams in Java 8
Java 8 groupingBy Example
How to sort a Map using Java8
Java 8 How to get common elements from two lists
Java 8 Stream Filter Example with Objects
How to Filter null values from Java8 Stream
User defined sorting with Java 8 Comparator
Hibernate Filter Example Xml Configuration
AngularJs lowercase Filter Example
AngularJs Currency Filter Example
AngularJs Orderby Filter Example
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String