package org.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
public class FlatMap {
private static List computerClub = Arrays.asList(
new Student("2015134001", "小明", 15, "1501"),
new Student("2015134003", "小王", 14, "1503"),
new Student("2015134006", "小张", 15, "1501"),
new Student("2015134008", "小梁", 17, "1505")
);
private static List basketballClub = Arrays.asList(
new Student("2015134012", "小c", 13, "1503"),
new Student("2015134013", "小s", 14, "1503"),
new Student("2015134015", "小d", 15, "1504"),
new Student("2015134018", "小y", 16, "1505")
);
private static List pingpongClub = Arrays.asList(
new Student("2015134022", "小u", 16, "1502"),
new Student("2015134021", "小i", 14, "1502"),
new Student("2015134026", "小m", 17, "1504"),
new Student("2015134027", "小n", 16, "1504")
);
public static void main(String[] args) {
List> allClubStu = new ArrayList<>();
allClubStu.add(computerClub);
allClubStu.add(basketballClub);
allClubStu.add(pingpongClub);
// 心得和经验分享下:你得看flatmap的方法签名,搞明白输入什么,输出什么,这样子
比较好理解。flatMap的输出,也得是一个stream类型,faltMap做的事情,就是把流里的每一个元素,都放到一个一维数组(或集合)中。
// flatMap 一对多映射处理,深入到多个stream内部去处理子元素,统一输出
List studentList = allClubStu.stream().flatMap(e -> e.stream().filter(student -> student.getAge() > 15)).collect(Collectors.toList());
studentList.forEach(System.out::println);
System.out.println("我是漂亮的分割线");
// flatMap 一对多映射处理,深入到多个stream内部去处理子元素,统一输出
List studentList2 = allClubStu.stream().flatMap(e -> e.stream()).collect(Collectors.toList());
studentList2.forEach(System.out::println);
}
@Data
static class Student {
private String id;
private String name;
private Integer age;
private String code;
public Student() {
super();
}
public Student(String id, String name, Integer age, String code) {
super();
this.id = id;
this.name = name;
this.age = age;
this.code = code;
}
}
}
输出的内容
FlatMap.Student(id=2015134008, name=小梁, age=17, code=1505)
FlatMap.Student(id=2015134018, name=小y, age=16, code=1505)
FlatMap.Student(id=2015134022, name=小u, age=16, code=1502)
FlatMap.Student(id=2015134026, name=小m, age=17, code=1504)
FlatMap.Student(id=2015134027, name=小n, age=16, code=1504)
我是漂亮的分割线
FlatMap.Student(id=2015134001, name=小明, age=15, code=1501)
FlatMap.Student(id=2015134003, name=小王, age=14, code=1503)
FlatMap.Student(id=2015134006, name=小张, age=15, code=1501)
FlatMap.Student(id=2015134008, name=小梁, age=17, code=1505)
FlatMap.Student(id=2015134012, name=小c, age=13, code=1503)
FlatMap.Student(id=2015134013, name=小s, age=14, code=1503)
FlatMap.Student(id=2015134015, name=小d, age=15, code=1504)
FlatMap.Student(id=2015134018, name=小y, age=16, code=1505)
FlatMap.Student(id=2015134022, name=小u, age=16, code=1502)
FlatMap.Student(id=2015134021, name=小i, age=14, code=1502)
FlatMap.Student(id=2015134026, name=小m, age=17, code=1504)
FlatMap.Student(id=2015134027, name=小n, age=16, code=1504)
代码段2
package org.test;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import net.sf.json.JSONArray;
public class MapAndFloatMap {
public static void main(String[] args) {
List strs = Arrays.asList("好,好,学", "习,天,天", "向,上");
List strArray = strs.stream().map(str -> str.split(",")).collect(Collectors.toList());
JSONArray jsonArray = JSONArray.fromObject(strArray);
///strArray => [["好","好","学"],["习","天","天"],["向","上"]]
System.out.println("strArray => " + jsonArray.toString());
///
/ strList => [好, 好, 学, 习, 天, 天, 向, 上]
// flatMap与map的区别在于 flatMap是将一个流中的每个值都转成一个个流,然后再将这些流扁平化成为一个流 。
List strList = strs.stream().map(str -> str.split(",")).flatMap(Arrays::stream)
.collect(Collectors.toList());
System.out.println("strList => " + strList);
}
}
strArray => [["好","好","学"],["习","天","天"],["向","上"]]
strList => [好, 好, 学, 习, 天, 天, 向, 上]
flatMap说实话有点绕,心得分享下:你得看flatmap的方法签名,搞明白输入什么,输出什么,这样子才比较好理解。flatMap的输出,也得是一个stream类型,faltMap做的事情,就是把流里的每一个元素,都放到一个一维数组(或集合)中。
再次重申一下,看了下别人写的文章,加了一下自己的理解。原创不易,支持原创。
参考文章:
stream中的map和flatMap_爱琴孩的博客-CSDN博客_stream中map和flatmap
Stream 中的map 与flatMap 用法_java编程-IT的博客-CSDN博客