目录
主要内容
- 使用map返回另外的类型。
- 从Stream转换为IntStream、LongStream、DoubleStream。
- 使用flatMap将嵌套的流串联成一个流。
- 使用分组功能
- 根据生日计算年龄
一、主程序
package xyz.jangle.thread.test.n6_7.flatmap;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
import xyz.jangle.thread.test.n6_2.create.Person;
import xyz.jangle.thread.test.n6_2.create.PersonGenerator;
/**
* 6.7 转换流 flatMap(中间操作)
* 1、使用map返回另外的类型。
* 2、从Stream转换为IntStream、LongStream、DoubleStream。
* 3、使用flatMap将嵌套的流串联成一个流。
* 4、使用分组功能
* 5、根据生日计算年龄
*
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年9月6日 上午9:32:06
*
*/
public class M {
public static void main(String[] args) {
// 打印薪资(去重) 转换为DoubleStream
List<Person> persons = PersonGenerator.generatePersonList(20);
DoubleStream doubleStream = persons.parallelStream().mapToDouble(p -> p.getSalary());
doubleStream.distinct().forEach(d -> System.out.println(d));
// 薪资记录数量(去重)
doubleStream = persons.parallelStream().mapToDouble(p -> p.getSalary());
long size = doubleStream.distinct().count();
System.out.println("Size:" + size);
// map返回另外的类型
List<BasicPerson> basicPersons = persons.parallelStream().map(p -> {
BasicPerson bp = new BasicPerson();
bp.setName(p.getFirstName() + " " + p.getLastName());
bp.setAge(getAge(p.getBirthDate()));
return bp;
}).collect(Collectors.toList());
basicPersons.forEach(bp -> System.out.println(bp.getName() + ": age is " + bp.getAge()));
// flatMap合并流 ,并分组
System.out.println("使用flatMap和分组功能:");
List<String> file = FileGenerator.generateFile(100);
ConcurrentMap<String, Long> wordCount = file.parallelStream().flatMap(line -> Stream.of(line.split("[ ,.]")))
.filter(w -> w.length() > 0).sorted()
.collect(Collectors.groupingByConcurrent(e -> e, Collectors.counting()));
wordCount.forEach((k, v) -> System.out.println(k + ":" + v));
}
/**
* 根据生日计算年龄
*
* @author jangle
* @time 2020年9月6日 上午10:19:01
* @param birthDate
* @return
*/
private static long getAge(Date birthDate) {
LocalDate start = birthDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate now = LocalDate.now();
long age = ChronoUnit.YEARS.between(start, now);
return age;
}
}
二、基础人员信息类
package xyz.jangle.thread.test.n6_7.flatmap;
/**
* 人员基础信息类
*
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年9月6日 上午9:37:49
*
*/
public class BasicPerson {
private String name;
private long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAge() {
return age;
}
public void setAge(long age) {
this.age = age;
}
}
三、模拟文件类
package xyz.jangle.thread.test.n6_7.flatmap;
import java.util.ArrayList;
import java.util.List;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2020年9月6日 上午9:41:18
*
*/
public class FileGenerator {
/**
* 模拟文件
*/
public static List<String> generateFile(int size) {
ArrayList<String> file = new ArrayList<String>();
for (int i = 0; i < size; i++) {
file.add("Constructs an empty list with an initial capacity of ten.");
}
return file;
}
}
四、执行结果
89238.0
81186.0
26475.0
4286.0
49049.0
14089.0
91966.0
67984.0
58107.0
49961.0
43798.0
32139.0
76372.0
73645.0
51136.0
95136.0
51188.0
32900.0
31711.0
70952.0
Size:20
F W: age is 20
A T: age is 2
G T: age is 5
B W: age is 15
E X: age is 17
C U: age is 16
G Y: age is 25
D T: age is 10
C T: age is 25
D X: age is 29
C V: age is 3
F V: age is 23
B W: age is 28
A T: age is 18
B W: age is 21
B W: age is 28
A Y: age is 11
B Y: age is 26
A W: age is 2
A Y: age is 10
使用flatMap和分组功能:
with:100
initial:100
of:100
list:100
ten:100
an:200
Constructs:100
capacity:100
empty:100