Java 8 Map filter examples

This article shows a few examples of filtering a Map by its keys or values using the Java 8 stream APIs.

Table of contents:

1. Java Map filter (Classic)

Before Java 8, we looped the Map, filtered it, and collected it manually.

MapFilterExample.java

package com.mkyong.examples;

import java.util.HashMap;
import java.util.Map;

public class MapFilterExample {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(1, "linode");
        map.put(2, "heroku");
        map.put(3, "aws");

        String result = "";
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            if ("linode".equals(entry.getValue())) {
                result = entry.getValue();
            }
        }

        // linode
        System.out.println(result);

    }

}

output


linode

2. Java Map filter (Java 8)

With Java 8, we can convert a Map.entrySet() into a stream, followed by a filter() and collect().

MapFilterExample2.java

package com.mkyong.examples;

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

public class MapFilterExample2 {

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(1, "linode");
        map.put(2, "heroku");
        map.put(3, "aws");

        //Map -> Stream -> Filter -> String
        String result1 = map.values().stream()
                .filter("linode"::equals)
                .collect(Collectors.joining());

        // linode
        System.out.println(result1);

        //Map -> Stream -> Filter -> MAP
        Map<Integer, String> result2 = map.entrySet().stream()
                .filter(x -> x.getKey() == 1)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        // k:1 v:linode
        result2.forEach((k, v) -> System.out.println("k:" + k + " v:" + v));

        // or like this
        Map<Integer, String> result3 = map.entrySet().stream()
                .filter(x -> x.getKey() == 3)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

        //k:3 v:aws
        result3.forEach((k, v) -> System.out.println("k:" + k + " v:" + v));

    }

}

output


linode
k:1 v:linode
k:3 v:aws

3. Java Map filter (Java 8 Predicate)

Java 8 map filtering using Predicate.

MapFilterExample3.java

package com.mkyong.examples;

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

public class MapFilterExample3 {

    // Generic Map filterByValue, with predicate
    public static <K, V> Map<K, V> filterByValue(Map<K, V> map, Predicate<V> predicate) {
        return map.entrySet()
                .stream()
                .filter(x -> predicate.test(x.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    public static void main(String[] args) {

        Map<Integer, String> map = new HashMap<>();
        map.put(1, "linode");
        map.put(2, "heroku");
        map.put(3, "aws");

        //  {1=linode.com}
        Map<Integer, String> filteredMap = filterByValue(map, x -> x.contains("linode"));
        System.out.println(filteredMap);

        // {1=linode, 3=aws}
        Map<Integer, String> filteredMap2 = filterByValue(map, x -> (x.contains("aws") || x.contains("linode")));
        System.out.println(filteredMap2);

        // {3=aws}
        Map<Integer, String> filteredMap3 = filterByValue(map, x -> (x.length() <= 5));
        System.out.println(filteredMap3);

    }

}

output


{1=linode}
{1=linode, 3=aws}
{3=aws}

4. References

2 comments on “Java 8 Map filter examples

  1. Hi Mykong in Each and Every post you simply posting the programs….Its better you can explain the concept and put some example programs by doing this way users will understand the concept….

    Reply
    1. I believe this is what make this place unique, only simple example. For explanation, you can find in the official documentation easily.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *