Java在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
[更多]
显示目录

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为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。