How to Filter a Map in Java 8

By | 2017-04-15T16:13:56+00:00 April 3rd, 2017|java8|0 Comments

In this tutorials, 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 condition in side the loop based on requirement.

Legacy Style to Filter a Map :

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.

Filter a Map in Java 8 :

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.

[box type=”shadow” align=”alignleft” class=”” width=”100%”]

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

[/box]

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);
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]
Filtered With Value Samsung
Filtered With Key : {2=iPhone 6S}

[/box]

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment