Java 8 – Filter a Map examples 修改教程

An example to show you how to filter a Map with Java 8 stream API.

Before Java 8 :

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

With Java 8, you can convert a Map.entrySet() to a stream, follow by a filter() and collect() it.

//Map -> Stream -> Filter -> String
    String result = A_MAP_EXAMPLE.entrySet().stream()
        .filter(map -> "something".equals(map.getValue()))
        .map(map->map.getValue())
        .collect(Collectors.joining());

    //Map -> Stream -> Filter -> MAP
    Map<Integer, String> collect = A_MAP_EXAMPLE.entrySet().stream()
        .filter(map -> map.getKey() == 2)
        .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

1. Java 8 – Filter a Map

A full example to filter a Map and return a String.

TestMapFilter.java

package com.mkyong.java8

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

public class TestMapFilter {

    public static void main(String[] args) {

        Map<Integer, String> HOSTING = new HashMap<>();
        HOSTING.put(1, "linode.com");
        HOSTING.put(2, "heroku.com");
        HOSTING.put(3, "digitalocean.com");
        HOSTING.put(4, "aws.amazon.com");

        String result = "";
        for (Map.Entry<Integer, String> entry : HOSTING.entrySet()) {
            if ("aws.amazon.com".equals(entry.getValue())) {
                result = entry.getValue();
            }
        }
        System.out.println("Before Java 8 : " + result);

        //Map -> Stream -> Filter -> String
        result = HOSTING.entrySet().stream()
                .filter(map -> "aws.amazon.com".equals(map.getValue()))
                .map(map -> map.getValue())
                .collect(Collectors.joining());

        System.out.println("With Java 8 : " + result);

    }
}

Output

Before Java 8 : aws.amazon.com
With Java 8 : aws.amazon.com

2. Java 8 – Filter a Map #2

Yet another example to filter a Map, but this will return a Map.

TestMapFilter.java

package com.mkyong.java8

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

public class TestMap {

    public static void main(String[] args) {

        Map<Integer, String> HOSTING = new HashMap<>();
        HOSTING.put(1, "linode.com");
        HOSTING.put(2, "heroku.com");
        HOSTING.put(3, "digitalocean.com");
        HOSTING.put(4, "aws.amazon.com");

        //Map -> Stream -> Filter -> Map
        Map<Integer, String> collect = HOSTING.entrySet().stream()
                .filter(map -> map.getKey() == 2)
                .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

        System.out.println(collect); //output : {2=heroku.com}

    }
}

References

  1. Processing Data with Java SE 8 Streams
  2. Java Collectors JavaDoc
  3. Java 8 Streams filter examples

如果觉得JSRUN的教程对您有用,请随意打赏。您的支持将鼓励JSRUN继续提供更好的服务

捐助JSRUN
JSRUN前端教程共编系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的本前端教程,让知识的积累变得统一完整、自成体系。 希望广大工程师能一起参与进共编,让零散在世界各处的知识点找到组织、找到家。
X
支付宝
9.99
请使用支付宝扫码支付, 有效时间 5分0秒
超过有效时间后请立即停止支付。
支付故障请联系QQ:565830900
因10.00元通道已占用,系统建议您支付9.99元
正在生成二维码, 此过程可能需要15秒钟
谢谢支持,我一定会更加努力的