时间有点仓促,未来及添加各部分详细说明。。。
1 public class StreamNotes {
2 public static void main(String[] args) {
3 /*
4 * 一、Stream的创建
5 * 1、Stream.of()方法可以接收可变参数、数组、列表
6 * 2、Array.stream(array, from, to)方法可以将array切片后的流
7 * 3、Stream.empty()方法返回一个空流
8 * 4、Collector.stream(),任何列表都可以通过stream()方法创建
9 * 5、Stream.generate()方法用于创建无限流,其接收一个供给型函数式接口的引元
10 * 6、Stream.iterate()方法接收2个参数,种子和f(x)
11 */
12
13 /*
14 * 二、Stream管道
15 * 1、过滤 filter
16 * 2、转换 map
17 * 3、合并 flatMap
18 * 4、抽取 limit skip peek
19 * 5、链接 concat
20 */
21
22 /*
23 * 三、收集结果
24 * 1、toArray()方法返回Object数组,若要返回指定类型数组,需传入构造器引用,如String::new
25 * 2、Collectors.toList()/toSet()方法返回列表/集合
26 * 3、Collectors.toCollector()方法接收构造器引用,来获取指定列表类型,如TreeSet::new
27 * 4、Collectors.joining()方法返回指定分隔符连接后的字符串
28 * 5、Collectors.summarizingInt|Long|Double()方法返回包含(最大值、最小值、平均值、求和)的
29 * (Int|Long|Double)SummaryStatistic对象,可以通过getMax()、getMin()...方法获取相应的值
30 */
31
32 /*
33 * 四、收集结果到映射表 toMap()
34 * 1、两个引元:两个引元分别代表Map的键和值,简单转化为Map
35 * 2、三个引元:当值存在冲突时,引入第三引元,来完成值的消重或收集操作
36 * 3、四个引元:指定集合类型
37 */
38 People[] people = {
39 new People("张", "飞", "男", 37),
40 new People("张", "三", "男", 30),
41 new People("李", "四", "男", 28),
42 new People("孙", "越", "女", 35)
43 };
44 Map<String, People> peopleMap = Stream.of(people).
45 collect(Collectors.toMap(
46 /* 前两个引元,提供键值对 */ People::getSurName, Function.identity(),
47 /* 第三个引元,消除重复值 */ (x, y) -> y, // 也可以创建集合,将重复值收集起来
48 /* 第四个引元,指定映射表 */ HashMap::new));
49 System.out.println(peopleMap.get("张"));
50
51 /*
52 * 五、约简(终止操作)
53 * 1、count()
54 * 2、max() min()
55 * 3、findFirst() findAny()
56 * 4、anyMatch、allMatch()、noneMatch()
57 * 5、reduce()
58 */
59
60 /*
61 * 六、Optional对象的创建和使用
62 * 1、说明:Optional对象对结果进行包装,以防止对null引用
63 * 2、创建:of()、empty()、ofNullable()
64 * 3、使用:
65 * ①orElse(),结果为null时,指定一个默认值
66 * ②orElseGet(),通过给定函数来计算默认值
67 * ③orElseThrow(),没有任何值时抛出异常
68 * ④ifPresent(),当值存在时,对值进行操作
69 * ⑤ifPresent()无法对返回值进行操作,若需要操作返回值,则直接使用map()
70 */
71
72 /*
73 * 七、群组和分区与下游收集器
74 * 1、groupingBy()
75 * ①单引元,根据给定函数返回的关键字,将流进行分组
76 * ②通过Collectors提供的工厂方法,完成对分组后数据的收集或求值
77 * ③求值时,若传入函数无法完成匹配时,需利用mapping完成操作
78 * 2、partitioningBy
79 */
80
81 /*
82 * 八、基本类型流 IntStream、LongStream、DoubleStream
83 * 1、对象流与基本类型流的互相转换
84 * 2、基本类型流的特有约简操作
85 * 3、toArray()方法返回对应的基本类型数组
86 * 4、summaryStatistic()方法返回对应的Int|Long|DoubleSummaryStatistic对象
87 */
88
89 /*
90 * 九、并发流(略)ParallelStream
91 * 没细看,留待以后跟并发一起总结
92 */
93
94 }
95 }
示例中People类
public class People {
private final String surName;
private String lastName;
private final String sex;
private int age;
private boolean isEmployed = false;
public People(String surName, String lastName, String sex, int age) {
this.surName = surName;
this.lastName = lastName;
this.sex = sex;
this.age = age;
}
public People() {
surName = "";
lastName = "老妖";
sex = "太监";
age = 1000;
}
public String getSurName() {
return surName;
}
public String getLastName() {
return lastName;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public boolean isEmployed() {
return isEmployed;
}
public void setEmployed(boolean employed) {
isEmployed = employed;
}
@Override
public String toString() {
String job = isEmployed ? "在职" : "待业";
return surName + lastName + ":" + sex + " " + age + "岁 " + job;
}
public static void main(String[] args) {
var p1 = new People("华", "罗庚", "男", 100);
// p.setEmployed(true);
var p2 = new People();
System.out.println(p1 + "\n" + p2);
}
}