Java8 - Stream map实例

596 阅读3分钟

java8 features - Stream map with examples

什么是java8中的流映射?

map()方法将对象映射到相同类型或不同类型的其他对象。

它是处理对象流的一个中间操作。

它们被懒洋洋地调用,并接受作为函数的输入和输出的对象流,其中包含对流中每个元素应用函数的结果。

以下是一个语法

  Stream map(Function mapper)

map 方法接受 Function作为输入。 Function是一个带有一个抽象方法的功能接口。接受T类型的输入并返回R类型的输出。

java地图过滤器的例子

这是一个List of String的例子,它包含数字的输入,输出返回偶数:

  • 创建了一个字符串的列表
  • 从列表中创建流
  • 用一个lambda表达式调用map()方法来遍历元素流
  • 对每个元素应用过滤器以检查事件编号
  • 最后,通过终端操作收集,将流转换为List
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List listStringNumbers = Arrays.asList("11", "12", "31", "41", "15", "16");
        System.out.println(listStringNumbers);
        List eventNumbers = listStringNumbers.stream()
                .map(s -> Integer.valueOf(s))
                .filter(number -> number % 2 == 0)
                .collect(Collectors.toList());
        System.out.println(eventNumbers);
    }
}  

输出

[11, 12, 31, 41, 15, 16]  
[12, 16]  

如何使用流将Map转换为List?

这是一个将hashmap 转换为自定义javaobject 的例子。
使用流的lambda表达式,迭代hash map元素并构造对象。

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MapExample {
    public static void main(String[] args) {
        Map map = new HashMap<>();
        map.put("1", "Kiran");
        map.put("12", "Ram");
        map.put("13", "John");
        map.put("5", "Frank");
        List listOfObjects = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
                .map(e -> new MyObject(e.getKey(), e.getValue())).collect(Collectors.toList());

        listOfObjects.forEach(l -> System.out.println("Id: " + l.getId() + ", Name: " + l.getName()));
    }
}
class MyObject {
    private String id;
    private String name;

    public MyObject(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
}  

输出

Id: 1, Name: Kiran  
Id: 12, Name: Ram  
Id: 13, Name: John  
Id: 5, Name: Frank  

如何将List of String转换为大写或小写?

从List创建一个Stream,在每个字符串上调用map,转换并输出到控制台。

import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List listString= Arrays.asList("one", "two", "three", "four", "five");
        listString.stream().map(String::toUpperCase).forEach(System.out::println);
    }
}

输出

ONE  
TWO  
THREE  
FOUR  
FIVE  

java8提供了三种类型的map 方法,用于处理intlongDouble 的值。

mapToInt - IntStream mapToInt ( ToIntFunction mapper )

  • 它接受ToIntFunction作为参数并返回IntStream
  • ToIntFunction是一个功能接口,有一个抽象方法,接受整数值并生成一个结果。
  • IntStream是Stream类的整数版本。
  • 流中的每个元素都被应用到函数中。

mapToLong - LongStream mapToLong ( ToLongFunction mapper )

  • 它接受ToLongFunction作为一个参数并返回LongStream
  • ToLongFunction是一个功能接口,它有一个抽象的方法,接收输入的长值并输出结果。
  • LongStream是Stream类的一个长版本。
  • 流中的每个元素都被应用到函数中

mapToDouble - DoubleStream mapToDouble ( ToDoubleFunction mapper )

  • 它接受ToDoubleFunction作为参数并返回DoubleStream
  • ToDoubleFunction是一个功能接口,它有一个抽象的方法,接受输入的双倍值并输出结果。
  • DoubleStream是 Stream 类的一个双倍版本。
  • 流中的每个元素都被应用到函数中。

总结

在本教程中,通过实例和方法解释学习了Stream map方法。