Java8的stream API

Posted by Zeusro on March 9, 2018
👈🏻 Select language
  1. 前期准备
    • 定义实体
    • 定义集合
  2. stream的其他用法
  3. stream的注意事项
    • 流只能用一次,重复使用会导致以下异常
    • filter
  4. 完整代码
  5. 参考链接

前期准备

  • 定义实体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 
package com.zeusro;


import java.util.Date;
import java.util.List;

/**
 * The type Person <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.08 <p/>
 */
public class Person implements Cloneable {

    // 身高
    private int height;
    //体重-
    private int weight;
    //身份证号
    private String identifier;
    //地址
    private String address;
    //生日
    private Date birthday;
    //爱好
    private List<String> hobbies;

    //性别
    private Sex sex;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }


}
//
1
2
3
4
5
6
7
8
9
10
11
package com.zeusro;

public enum Sex {
    //男
    Male,
    //女
    Female,
    //第三性 https://zh.wikipedia.org/wiki/%E7%AC%AC%E4%B8%89%E6%80%A7
    X,
}
  • 定义集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("北京");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("逛街");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("北京");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("北京");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("上網");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("北京");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
//

stream的其他用法

用于校验集合(引用自IBM)

allMatch:Stream 中全部元素符合传入的 predicate,返回 true

anyMatch:Stream 中只要有一个元素符合传入的 predicate,返回 true

noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true

自己生成流(引用自IBM)

  • Stream.generate

通过实现 Supplier 接口,你可以自己来控制流的生成。这种情形通常用于随机数、常量的 Stream,或者需要前后元素间维持着某种状态信息的 Stream。把 Supplier 实例传递给 Stream.generate() 生成的 Stream,默认是串行(相对 parallel 而言)但无序的(相对 ordered 而言)。由于它是无限的,在管道中,必须利用 limit 之类的操作限制 Stream 大小。

1
2
3
4
5
6
Random seed = new Random();
Supplier<Integer> random = seed::nextInt;
Stream.generate(random).limit(10).forEach(System.out::println);
//Another way
IntStream.generate(() -> (int) (System.nanoTime() % 100)).
limit(10).forEach(System.out::println);

Stream.generate() 还接受自己实现的 Supplier。例如在构造海量测试数据的时候,用某种自动的规则给每一个变量赋值;或者依据公式计算 Stream 的每个元素值。这些都是维持状态信息的情形。

1
2
3
4
5
6
7
8
9
10
11
Stream.generate(new PersonSupplier()).
limit(10).
forEach(p -> System.out.println(p.getName() + ", " + p.getAge()));
private class PersonSupplier implements Supplier<Person> {
 private int index = 0;
 private Random random = new Random();
 @Override
 public Person get() {
 return new Person(index++, "StormTestUser" + index, random.nextInt(100));
 }
}
1
2
3
4
5
6
7
8
9
10
11
# 输出结果:
StormTestUser1, 9
StormTestUser2, 12
StormTestUser3, 88
StormTestUser4, 51
StormTestUser5, 22
StormTestUser6, 28
StormTestUser7, 81
StormTestUser8, 51
StormTestUser9, 4
StormTestUser10, 76
  • Stream.iterate

iterate 跟 reduce 操作很像,接受一个种子值,和一个 UnaryOperator(例如 f)。然后种子值成为 Stream 的第一个元素,f(seed) 为第二个,f(f(seed)) 第三个,以此类推。

1
Stream.iterate(0, n -> n + 3).limit(10). forEach(x -> System.out.print(x + " "));.
1
2
# 输出结果:
0 3 6 9 12 15 18 21 24 27

与 Stream.generate 相仿,在 iterate 时候管道必须有 limit 这样的操作来限制 Stream 大小。

stream的注意事项

  • 流只能用一次,重复使用会导致以下异常
1
2
3
Stream<Person> list1Stream = list1.stream();
list1Stream.filter(o -> o.getBirthday().equals(time1)).count();
list1Stream.filter(o -> !o.getBirthday().equals(time1)).count();//java.lang.IllegalStateException: stream has already been operated upon or closed

所以我建议每次需要集合操作的时候都直接新建一个 stream, 而不是使用Stream<Person> list1Stream = list1.stream();去定义

1
2
list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
list1.stream().filter(o -> !o.getBirthday().equals(time1)).count();
  • filter

一般有filter 操作时,不用并行流parallelStream ,如果用的话可能会导致线程安全问题

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// 

import com.google.gson.Gson;
import com.zeusro.Person;
import com.zeusro.Sex;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;


/**
 * The type Main <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.09 <p/>
 */
public class Main {


    private static Date convertLocalDateToTimeZone(LocalDate localDate) {
        ZoneId zoneID = ZoneId.systemDefault();
        return Date.from(localDate.atStartOfDay(zoneID).toInstant());
    }

    public static void main(String[] args) {

        try {
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("北京");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("逛街");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("北京");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("北京");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("上網");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("北京");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
            //流只能用一次,重复使用会导致以下异常
            //java.lang.IllegalStateException: stream has already been operated upon or closed
            Stream<Person> list1Stream = list1.stream();
            Date time1 = convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1));
            //0
            Long count1 = list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
            Map<Sex, List<Person>> group1 = list1.stream().collect(Collectors.groupingBy(Person::getSex));
            Iterator it = group1.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Sex, List<Person>> groupByItem = (Map.Entry) it.next();
                Sex sex = groupByItem.getKey();
                out.println(sex);
                groupByItem.getValue().forEach(person -> {
                    out.println(new Gson().toJson(person));
                });
            }
/*
输出结果:
Male
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}
Female
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"Female"}
X
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
 */
            //stream没有RemoveAll的操作
            Person after90 = list1.stream()
                    .filter(o -> o.getBirthday().after(convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1))))
                    .findFirst()
                    .orElse(null);
            // null
            list1.stream().forEach(o -> {
                //在ForEach當中可對集合進行操作
                o.setSex(Sex.X);
            });
            list1.forEach(o -> {
                out.println(new Gson().toJson(o));
            });
/*
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
 */
            //IntStream的max方法返回的是OptionalInt,要先判断有没有值再读取值.isPresent=false 时直接getAsInt会报错.mapToLong,mapToDouble同理
            OptionalInt maxHeightOption = list1.stream().mapToInt(Person::getHeight).max();
            //字符串拼接、数值的 sum、min、max、average 都是特殊的 reduce。
            //当集合为长度0的集合时会返回起始值Integer.MIN_VALUE,起始值也不能乱传,个中缘由我暂不清楚
            int maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            out.println(maxHeight);
            //170
            if (maxHeightOption.isPresent()) {
                maxHeight = maxHeightOption.getAsInt();
                out.println(maxHeight);
                //170
            }
            //mapToInt参数的2种写法都一样,我比较喜欢以下写法,但是 idea 会报 warning
            OptionalInt minWeightOption = list1.stream().mapToInt(o -> o.getHeight()).min();
            int minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            list1.stream().map(Person::getIdentifier).distinct();
            //skip和 limit参数都是long, 这个要注意
            list1.stream().skip(1L).limit(2L);
            out.println("------------------------------------|升序|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday)).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> left.getBirthday().compareTo(right.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            /*
[{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}]
[{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}]            
             */
            out.println("------------------------------------|降序|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday).reversed()).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> right.getBirthday().compareTo(left.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
/*
[{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}]
[{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}]
 */
            out.println("------------------------------------|交集 list1 ∩ list2|------------------------------------");
            list1.stream().filter(o -> list2.contains(o)).collect(Collectors.toList());
            out.println("------------------------------------|并集list1 ∪ list2 |------------------------------------");
            list1.addAll(list2);
            list1.stream().distinct().collect(Collectors.toList());
            out.println("------------------------------------|差集list1 - list2|------------------------------------");
            list1.stream().filter(item1 -> !list2.contains(item1)).collect(Collectors.toList());
            out.println("------------------------------------|数据结构转换|------------------------------------");
            List<Person> list3 = list1.stream().filter(o -> true).collect(Collectors.toList());
            ArrayList<Person> list4 = list1.stream().filter(o -> true).collect(Collectors.toCollection(ArrayList::new));
            Set<Person> list5 = list1.stream().filter(o -> true).collect(Collectors.toSet());
            Object[] list6 = list1.stream().toArray();
            Person[] list7 = list1.stream().toArray(Person[]::new);
            out.println("------------------------------------|其他需要注意的地方|------------------------------------");
            //2个数组合并用这种方法的话, list2会为空
            Stream.of(list1, list2).collect(Collectors.toList());
            //以下才是正确用法
            Stream.of(list1, list2).flatMap(List::stream).collect(Collectors.toList());
            out.println("------------------------------------|reduce|------------------------------------");
            //字符串拼接、数值的 sum、min、max、average 都是特殊的 reduce。
            maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            int sumWeight = -1;
            OptionalInt sumWeightOption = list1.stream().mapToInt(Person::getHeight).reduce(Integer::sum);
            if (sumWeightOption.isPresent()) {
                sumWeight = sumWeightOption.getAsInt();
            }
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, (a, b) -> a + b);
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, Integer::sum);

            out.println("------------------------------------|peek ,forEach ,forEachOrdered |------------------------------------");
            out.println("forEach后面无法继续执行方法");
            list1.stream().forEach(o -> {
                out.println(new Gson().toJson(o));
            });
            /*
forEach后面无法继续执行方法
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"北京","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}            
             */
            out.println("先排序,然后遍历");
            list1.stream().forEachOrdered(o -> {
                out.println(new Gson().toJson(o));
            });
/*
先排序,然后遍历
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"北京","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}
 */
            out.println("peek*2");
            List<Integer> l = Stream.iterate(0, (Integer n) -> n + 1) //生成一个每次递增1的等差数列
                    .peek(n -> out.println("number generated:" + n))
                    .filter(n -> (n % 2 == 0))
                    .peek(n -> out.println("Even number filter passed for" + n)) //遇到偶数时继续输出
                    .limit(5).collect(Collectors.toList());
            out.println(new Gson().toJson(l));
            //可以看到在生成到9时,这个操作结束了,是因为设置了limit
            /*
peek*2
number generated:0
Even number filter passed for0
number generated:1
number generated:2
Even number filter passed for2
number generated:3
number generated:4
Even number filter passed for4
number generated:5
number generated:6
Even number filter passed for6
number generated:7
number generated:8
Even number filter passed for8
[0,2,4,6,8]            
             */
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.println("done");
        }
    }


}

// 

参考链接:

  1. Java 8 中的 Streams API 详解
  2. Introduction to the Java 8 Date/Time API
  3. Convert java.time.LocalDate into java.util.Date type
  4. Java 8之Stream API
  5. Comparator.comparing(…) throwing non-static reference exception while taking String::compareTo
  6. 采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集
  7. 详解Java中的clone方法 – 原型模式
  8. Collection to stream to a new collection
  9. Java parallel stream用法
  10. [Java 8 – How to ‘peek’ into a running Stream Stream.peek method tutorial with examples](https://www.javabrahman.com/java-8/java-8-how-to-peek-into-a-running-stream-peek-method-tutorial-with-examples/)
  11. JDK8函数式接口Function、Consumer、Predicate、Supplier
  1. Preparation
    • Define Entity
    • Define Collection
  2. Other Uses of Stream
  3. Notes on Stream
    • Streams can only be used once, reusing will cause the following exception
    • filter
  4. Complete Code
  5. References

Preparation

  • Define Entity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 
package com.zeusro;


import java.util.Date;
import java.util.List;

/**
 * The type Person <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.08 <p/>
 */
public class Person implements Cloneable {

    // Height
    private int height;
    // Weight
    private int weight;
    // ID number
    private String identifier;
    // Address
    private String address;
    // Birthday
    private Date birthday;
    // Hobbies
    private List<String> hobbies;

    // Gender
    private Sex sex;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }


}
//
1
2
3
4
5
6
7
8
9
10
11
package com.zeusro;

public enum Sex {
    // Male
    Male,
    // Female
    Female,
    // Third gender https://zh.wikipedia.org/wiki/%E7%AC%AC%E4%B8%89%E6%80%A7
    X,
}
  • Define Collection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("Beijing");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Shopping");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("Beijing");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Watching movies");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("Beijing");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Surfing the internet");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("Beijing");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Watching movies");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
//

Other Uses of Stream

For Validating Collections (cited from IBM)

allMatch: Returns true if all elements in the Stream match the given predicate

anyMatch: Returns true if any element in the Stream matches the given predicate

noneMatch: Returns true if no elements in the Stream match the given predicate

Generate Stream Yourself (cited from IBM)

  • Stream.generate

By implementing the Supplier interface, you can control the generation of the stream yourself. This is typically used for random numbers, constant Streams, or Streams that need to maintain some state information between elements. The Stream generated by passing a Supplier instance to Stream.generate() is serial (relative to parallel) but unordered (relative to ordered) by default. Since it is infinite, you must use operations like limit in the pipeline to restrict the Stream size.

1
2
3
4
5
6
Random seed = new Random();
Supplier<Integer> random = seed::nextInt;
Stream.generate(random).limit(10).forEach(System.out::println);
//Another way
IntStream.generate(() -> (int) (System.nanoTime() % 100)).
limit(10).forEach(System.out::println);

Stream.generate() also accepts your own Supplier implementation. For example, when constructing massive test data, use some automatic rule to assign values to each variable; or calculate each element value of the Stream according to a formula. These are all cases where state information is maintained.

1
2
3
4
5
6
7
8
9
10
11
Stream.generate(new PersonSupplier()).
limit(10).
forEach(p -> System.out.println(p.getName() + ", " + p.getAge()));
private class PersonSupplier implements Supplier<Person> {
 private int index = 0;
 private Random random = new Random();
 @Override
 public Person get() {
 return new Person(index++, "StormTestUser" + index, random.nextInt(100));
 }
}
1
2
3
4
5
6
7
8
9
10
11
# Output:
StormTestUser1, 9
StormTestUser2, 12
StormTestUser3, 88
StormTestUser4, 51
StormTestUser5, 22
StormTestUser6, 28
StormTestUser7, 81
StormTestUser8, 51
StormTestUser9, 4
StormTestUser10, 76
  • Stream.iterate

iterate is very similar to the reduce operation, accepting a seed value and a UnaryOperator (e.g., f). Then the seed value becomes the first element of the Stream, f(seed) is the second, f(f(seed)) is the third, and so on.

1
Stream.iterate(0, n -> n + 3).limit(10). forEach(x -> System.out.print(x + " "));.
1
2
# Output:
0 3 6 9 12 15 18 21 24 27

Similar to Stream.generate, when using iterate, the pipeline must have operations like limit to restrict the Stream size.

Notes on Stream

  • Streams can only be used once, reusing will cause the following exception
1
2
3
Stream<Person> list1Stream = list1.stream();
list1Stream.filter(o -> o.getBirthday().equals(time1)).count();
list1Stream.filter(o -> !o.getBirthday().equals(time1)).count();//java.lang.IllegalStateException: stream has already been operated upon or closed

So I recommend creating a new stream directly each time you need collection operations, rather than using Stream<Person> list1Stream = list1.stream(); to define it

1
2
list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
list1.stream().filter(o -> !o.getBirthday().equals(time1)).count();
  • filter

Generally, when there is a filter operation, do not use parallel streams (parallelStream), as it may cause thread safety issues

Complete Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 

import com.google.gson.Gson;
import com.zeusro.Person;
import com.zeusro.Sex;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;


/**
 * The type Main <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.09 <p/>
 */
public class Main {


    private static Date convertLocalDateToTimeZone(LocalDate localDate) {
        ZoneId zoneID = ZoneId.systemDefault();
        return Date.from(localDate.atStartOfDay(zoneID).toInstant());
    }

    public static void main(String[] args) {

        try {
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("Beijing");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Shopping");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("Beijing");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Watching movies");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("Beijing");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Surfing the internet");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("Beijing");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("Eating");
                add("Watching movies");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
            //Streams can only be used once, reusing will cause the following exception
            //java.lang.IllegalStateException: stream has already been operated upon or closed
            Stream<Person> list1Stream = list1.stream();
            Date time1 = convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1));
            //0
            Long count1 = list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
            Map<Sex, List<Person>> group1 = list1.stream().collect(Collectors.groupingBy(Person::getSex));
            Iterator it = group1.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Sex, List<Person>> groupByItem = (Map.Entry) it.next();
                Sex sex = groupByItem.getKey();
                out.println(sex);
                groupByItem.getValue().forEach(person -> {
                    out.println(new Gson().toJson(person));
                });
            }
/*
Output:
Male
{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"Male"}
Female
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"Female"}
X
{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}
 */
            //stream does not have RemoveAll operation
            Person after90 = list1.stream()
                    .filter(o -> o.getBirthday().after(convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1))))
                    .findFirst()
                    .orElse(null);
            // null
            list1.stream().forEach(o -> {
                //You can operate on the collection in ForEach
                o.setSex(Sex.X);
            });
            list1.forEach(o -> {
                out.println(new Gson().toJson(o));
            });
/*
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"}
{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}
 */
            //IntStream's max method returns OptionalInt, you must check if there is a value before reading it. getAsInt will throw an error when isPresent=false. Same applies to mapToLong, mapToDouble
            OptionalInt maxHeightOption = list1.stream().mapToInt(Person::getHeight).max();
            //String concatenation, sum, min, max, average of numbers are all special cases of reduce.
            //When the collection is of length 0, it will return the initial value Integer.MIN_VALUE. The initial value cannot be passed arbitrarily, the reason for which I am not clear about yet
            int maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            out.println(maxHeight);
            //170
            if (maxHeightOption.isPresent()) {
                maxHeight = maxHeightOption.getAsInt();
                out.println(maxHeight);
                //170
            }
            //The two ways of writing mapToInt parameters are the same, I prefer the following way, but idea will report a warning
            OptionalInt minWeightOption = list1.stream().mapToInt(o -> o.getHeight()).min();
            int minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            list1.stream().map(Person::getIdentifier).distinct();
            //Note that skip and limit parameters are both long
            list1.stream().skip(1L).limit(2L);
            out.println("------------------------------------|Ascending|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday)).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> left.getBirthday().compareTo(right.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            /*
[{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}]
[{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}]            
             */
            out.println("------------------------------------|Descending|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday).reversed()).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> right.getBirthday().compareTo(left.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
/*
[{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}]
[{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}]
 */
            out.println("------------------------------------|Intersection list1 ∩ list2|------------------------------------");
            list1.stream().filter(o -> list2.contains(o)).collect(Collectors.toList());
            out.println("------------------------------------|Union list1 ∪ list2 |------------------------------------");
            list1.addAll(list2);
            list1.stream().distinct().collect(Collectors.toList());
            out.println("------------------------------------|Difference list1 - list2|------------------------------------");
            list1.stream().filter(item1 -> !list2.contains(item1)).collect(Collectors.toList());
            out.println("------------------------------------|Data Structure Conversion|------------------------------------");
            List<Person> list3 = list1.stream().filter(o -> true).collect(Collectors.toList());
            ArrayList<Person> list4 = list1.stream().filter(o -> true).collect(Collectors.toCollection(ArrayList::new));
            Set<Person> list5 = list1.stream().filter(o -> true).collect(Collectors.toSet());
            Object[] list6 = list1.stream().toArray();
            Person[] list7 = list1.stream().toArray(Person[]::new);
            out.println("------------------------------------|Other Things to Note|------------------------------------");
            //If merging two arrays this way, list2 will be empty
            Stream.of(list1, list2).collect(Collectors.toList());
            //The following is the correct usage
            Stream.of(list1, list2).flatMap(List::stream).collect(Collectors.toList());
            out.println("------------------------------------|reduce|------------------------------------");
            //String concatenation, sum, min, max, average of numbers are all special cases of reduce.
            maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            int sumWeight = -1;
            OptionalInt sumWeightOption = list1.stream().mapToInt(Person::getHeight).reduce(Integer::sum);
            if (sumWeightOption.isPresent()) {
                sumWeight = sumWeightOption.getAsInt();
            }
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, (a, b) -> a + b);
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, Integer::sum);

            out.println("------------------------------------|peek ,forEach ,forEachOrdered |------------------------------------");
            out.println("forEach cannot continue executing methods after it");
            list1.stream().forEach(o -> {
                out.println(new Gson().toJson(o));
            });
            /*
forEach cannot continue executing methods after it
{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"Beijing","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"Male"}            
             */
            out.println("Sort first, then iterate");
            list1.stream().forEachOrdered(o -> {
                out.println(new Gson().toJson(o));
            });
/*
Sort first, then iterate
{"height":170,"weight":50,"identifier":"3","address":"Beijing","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["Eating","Surfing the internet"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"Beijing","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"Beijing","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["Eating","Shopping"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"Beijing","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["Eating","Watching movies"],"sex":"Male"}
 */
            out.println("peek*2");
            List<Integer> l = Stream.iterate(0, (Integer n) -> n + 1) //Generate an arithmetic sequence that increments by 1 each time
                    .peek(n -> out.println("number generated:" + n))
                    .filter(n -> (n % 2 == 0))
                    .peek(n -> out.println("Even number filter passed for" + n)) //Continue output when encountering even numbers
                    .limit(5).collect(Collectors.toList());
            out.println(new Gson().toJson(l));
            //You can see that when generating to 9, this operation ended because limit was set
            /*
peek*2
number generated:0
Even number filter passed for0
number generated:1
number generated:2
Even number filter passed for2
number generated:3
number generated:4
Even number filter passed for4
number generated:5
number generated:6
Even number filter passed for6
number generated:7
number generated:8
Even number filter passed for8
[0,2,4,6,8]            
             */
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.println("done");
        }
    }


}

// 

References:

  1. Java 8 中的 Streams API 详解
  2. Introduction to the Java 8 Date/Time API
  3. Convert java.time.LocalDate into java.util.Date type
  4. Java 8之Stream API
  5. Comparator.comparing(…) throwing non-static reference exception while taking String::compareTo
  6. 采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集
  7. 详解Java中的clone方法 – 原型模式
  8. Collection to stream to a new collection
  9. Java parallel stream用法
  10. [Java 8 – How to ‘peek’ into a running Stream Stream.peek method tutorial with examples](https://www.javabrahman.com/java-8/java-8-how-to-peek-into-a-running-stream-peek-method-tutorial-with-examples/)
  11. JDK8函数式接口Function、Consumer、Predicate、Supplier
  1. Подготовка
    • Определение сущности
    • Определение коллекции
  2. Другие способы использования stream
  3. Важные замечания по stream
    • Поток можно использовать только один раз, повторное использование приведет к следующему исключению
    • filter
  4. Полный код
  5. Ссылки

Подготовка

  • Определение сущности
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 
package com.zeusro;


import java.util.Date;
import java.util.List;

/**
 * The type Person <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.08 <p/>
 */
public class Person implements Cloneable {

    // Рост
    private int height;
    // Вес
    private int weight;
    // Номер удостоверения личности
    private String identifier;
    // Адрес
    private String address;
    // День рождения
    private Date birthday;
    // Хобби
    private List<String> hobbies;

    // Пол
    private Sex sex;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }


}
//
1
2
3
4
5
6
7
8
9
10
11
package com.zeusro;

public enum Sex {
    // Мужской
    Male,
    // Женский
    Female,
    // Третий пол https://zh.wikipedia.org/wiki/%E7%AC%AC%E4%B8%89%E6%80%A7
    X,
}
  • Определение коллекции
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("北京");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("逛街");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("北京");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("北京");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("上網");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("北京");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
//

Другие способы использования stream

Для проверки коллекций (цитируется по IBM)

allMatch: Возвращает true, если все элементы в Stream соответствуют переданному predicate

anyMatch: Возвращает true, если любой элемент в Stream соответствует переданному predicate

noneMatch: Возвращает true, если ни один элемент в Stream не соответствует переданному predicate

Самостоятельная генерация потока (цитируется по IBM)

  • Stream.generate

Реализуя интерфейс Supplier, вы можете самостоятельно контролировать генерацию потока. Это обычно используется для случайных чисел, константных Stream, или Stream, которым необходимо поддерживать некоторую информацию о состоянии между элементами. Stream, сгенерированный путем передачи экземпляра Supplier в Stream.generate(), по умолчанию является последовательным (относительно parallel), но неупорядоченным (относительно ordered). Поскольку он бесконечен, в конвейере необходимо использовать операции типа limit для ограничения размера Stream.

1
2
3
4
5
6
Random seed = new Random();
Supplier<Integer> random = seed::nextInt;
Stream.generate(random).limit(10).forEach(System.out::println);
//Another way
IntStream.generate(() -> (int) (System.nanoTime() % 100)).
limit(10).forEach(System.out::println);

Stream.generate() также принимает вашу собственную реализацию Supplier. Например, при построении больших объемов тестовых данных, используя какое-то автоматическое правило для присвоения значений каждой переменной; или вычисляя каждое значение элемента Stream по формуле. Это все случаи, когда поддерживается информация о состоянии.

1
2
3
4
5
6
7
8
9
10
11
Stream.generate(new PersonSupplier()).
limit(10).
forEach(p -> System.out.println(p.getName() + ", " + p.getAge()));
private class PersonSupplier implements Supplier<Person> {
 private int index = 0;
 private Random random = new Random();
 @Override
 public Person get() {
 return new Person(index++, "StormTestUser" + index, random.nextInt(100));
 }
}
1
2
3
4
5
6
7
8
9
10
11
# Результат вывода:
StormTestUser1, 9
StormTestUser2, 12
StormTestUser3, 88
StormTestUser4, 51
StormTestUser5, 22
StormTestUser6, 28
StormTestUser7, 81
StormTestUser8, 51
StormTestUser9, 4
StormTestUser10, 76
  • Stream.iterate

iterate очень похож на операцию reduce, принимает начальное значение и UnaryOperator (например, f). Затем начальное значение становится первым элементом Stream, f(seed) - вторым, f(f(seed)) - третьим, и так далее.

1
Stream.iterate(0, n -> n + 3).limit(10). forEach(x -> System.out.print(x + " "));.
1
2
# Результат вывода:
0 3 6 9 12 15 18 21 24 27

Аналогично Stream.generate, при использовании iterate в конвейере должны быть операции типа limit для ограничения размера Stream.

Важные замечания по stream

  • Поток можно использовать только один раз, повторное использование приведет к следующему исключению
1
2
3
Stream<Person> list1Stream = list1.stream();
list1Stream.filter(o -> o.getBirthday().equals(time1)).count();
list1Stream.filter(o -> !o.getBirthday().equals(time1)).count();//java.lang.IllegalStateException: stream has already been operated upon or closed

Поэтому я рекомендую каждый раз, когда нужны операции с коллекцией, создавать новый stream напрямую, а не использовать Stream<Person> list1Stream = list1.stream(); для определения

1
2
list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
list1.stream().filter(o -> !o.getBirthday().equals(time1)).count();
  • filter

Обычно, когда есть операция filter, не используйте параллельные потоки (parallelStream), так как это может привести к проблемам с потокобезопасностью

Полный код

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 

import com.google.gson.Gson;
import com.zeusro.Person;
import com.zeusro.Sex;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.out;


/**
 * The type Main <p/>
 * author: <a href="http://zeusro.github.io/">Zeusro</a> <p/>
 *
 * @date Created in 2018.03.09 <p/>
 */
public class Main {


    private static Date convertLocalDateToTimeZone(LocalDate localDate) {
        ZoneId zoneID = ZoneId.systemDefault();
        return Date.from(localDate.atStartOfDay(zoneID).toInstant());
    }

    public static void main(String[] args) {

        try {
            // LocalDate.of(2018, 02, 29);
            //java.time.DateTimeException: Invalid date 'February 29' as '2018' is not a leap year
            final Person female = new Person();
            female.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1981, 1, 1)));
            female.setHeight(165);
            female.setWeight(50);
            female.setSex(Sex.Female);
            female.setAddress("北京");
            female.setIdentifier("1");
            female.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("逛街");
            }});

            final Person male = new Person();
            male.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1982, 2, 1)));
            male.setHeight(170);
            male.setWeight(50);
            male.setSex(Sex.Male);
            male.setAddress("北京");
            male.setIdentifier("2");
            male.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            final Person x = new Person();
            x.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1983, 3, 1)));
            x.setHeight(170);
            x.setWeight(50);
            x.setSex(Sex.X);
            x.setAddress("北京");
            x.setIdentifier("3");
            x.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("上網");
            }});

            final Person male2 = new Person();
            male2.setBirthday(convertLocalDateToTimeZone(LocalDate.of(1984, 1, 1)));
            male2.setHeight(150);
            male2.setWeight(35);
            male2.setSex(Sex.Male);
            male2.setAddress("北京");
            male2.setIdentifier("4");
            male2.setHobbies(new ArrayList<String>() {{
                add("吃飯");
                add("看電影");
            }});

            List<Person> list1 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male);
                    add(x);
                }
            };
            List<Person> list2 = new ArrayList<Person>() {
                {
                    add(female);
                    add(male2);
                }
            };
            //Поток можно использовать только один раз, повторное использование приведет к следующему исключению
            //java.lang.IllegalStateException: stream has already been operated upon or closed
            Stream<Person> list1Stream = list1.stream();
            Date time1 = convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1));
            //0
            Long count1 = list1.stream().filter(o -> o.getBirthday().equals(time1)).count();
            Map<Sex, List<Person>> group1 = list1.stream().collect(Collectors.groupingBy(Person::getSex));
            Iterator it = group1.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Sex, List<Person>> groupByItem = (Map.Entry) it.next();
                Sex sex = groupByItem.getKey();
                out.println(sex);
                groupByItem.getValue().forEach(person -> {
                    out.println(new Gson().toJson(person));
                });
            }
/*
Результат вывода:
Male
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}
Female
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"Female"}
X
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
 */
            //stream не имеет операции RemoveAll
            Person after90 = list1.stream()
                    .filter(o -> o.getBirthday().after(convertLocalDateToTimeZone(LocalDate.of(1990, 1, 1))))
                    .findFirst()
                    .orElse(null);
            // null
            list1.stream().forEach(o -> {
                //В ForEach можно выполнять операции с коллекцией
                o.setSex(Sex.X);
            });
            list1.forEach(o -> {
                out.println(new Gson().toJson(o));
            });
/*
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
 */
            //Метод max класса IntStream возвращает OptionalInt, необходимо сначала проверить наличие значения перед его чтением. Прямой вызов getAsInt при isPresent=false приведет к ошибке. То же самое относится к mapToLong, mapToDouble
            OptionalInt maxHeightOption = list1.stream().mapToInt(Person::getHeight).max();
            //Конкатенация строк, sum, min, max, average чисел - все это особые случаи reduce.
            //Когда коллекция имеет длину 0, будет возвращено начальное значение Integer.MIN_VALUE. Начальное значение также нельзя передавать произвольно, причина этого мне пока не ясна
            int maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            out.println(maxHeight);
            //170
            if (maxHeightOption.isPresent()) {
                maxHeight = maxHeightOption.getAsInt();
                out.println(maxHeight);
                //170
            }
            //Два способа записи параметров mapToInt одинаковы, мне больше нравится следующий способ, но idea выдает warning
            OptionalInt minWeightOption = list1.stream().mapToInt(o -> o.getHeight()).min();
            int minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            list1.stream().map(Person::getIdentifier).distinct();
            //Обратите внимание, что параметры skip и limit оба имеют тип long
            list1.stream().skip(1L).limit(2L);
            out.println("------------------------------------|По возрастанию|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday)).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> left.getBirthday().compareTo(right.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            /*
[{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}]
[{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}]            
             */
            out.println("------------------------------------|По убыванию|------------------------------------");
            list1 = list1.stream().sorted(Comparator.comparing(Person::getBirthday).reversed()).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
            list1 = list1.stream().sorted((left, right) -> right.getBirthday().compareTo(left.getBirthday())).collect(Collectors.toList());
            out.println(new Gson().toJson(list1));
/*
[{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}]
[{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"},{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"},{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}]
 */
            out.println("------------------------------------|Пересечение list1 ∩ list2|------------------------------------");
            list1.stream().filter(o -> list2.contains(o)).collect(Collectors.toList());
            out.println("------------------------------------|Объединение list1 ∪ list2 |------------------------------------");
            list1.addAll(list2);
            list1.stream().distinct().collect(Collectors.toList());
            out.println("------------------------------------|Разность list1 - list2|------------------------------------");
            list1.stream().filter(item1 -> !list2.contains(item1)).collect(Collectors.toList());
            out.println("------------------------------------|Преобразование структуры данных|------------------------------------");
            List<Person> list3 = list1.stream().filter(o -> true).collect(Collectors.toList());
            ArrayList<Person> list4 = list1.stream().filter(o -> true).collect(Collectors.toCollection(ArrayList::new));
            Set<Person> list5 = list1.stream().filter(o -> true).collect(Collectors.toSet());
            Object[] list6 = list1.stream().toArray();
            Person[] list7 = list1.stream().toArray(Person[]::new);
            out.println("------------------------------------|Другие важные моменты|------------------------------------");
            //Если объединить два массива таким способом, list2 будет пустым
            Stream.of(list1, list2).collect(Collectors.toList());
            //Ниже правильный способ использования
            Stream.of(list1, list2).flatMap(List::stream).collect(Collectors.toList());
            out.println("------------------------------------|reduce|------------------------------------");
            //Конкатенация строк, sum, min, max, average чисел - все это особые случаи reduce.
            maxHeight = list1.stream().mapToInt(Person::getHeight).reduce(Integer.MIN_VALUE, Integer::max);
            minWeight = list1.stream().mapToInt(o -> o.getHeight()).reduce(Integer.MAX_VALUE, Integer::min);
            int sumWeight = -1;
            OptionalInt sumWeightOption = list1.stream().mapToInt(Person::getHeight).reduce(Integer::sum);
            if (sumWeightOption.isPresent()) {
                sumWeight = sumWeightOption.getAsInt();
            }
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, (a, b) -> a + b);
            sumWeight = list1.stream().mapToInt(Person::getHeight).reduce(0, Integer::sum);

            out.println("------------------------------------|peek ,forEach ,forEachOrdered |------------------------------------");
            out.println("После forEach нельзя продолжить выполнение методов");
            list1.stream().forEach(o -> {
                out.println(new Gson().toJson(o));
            });
            /*
После forEach нельзя продолжить выполнение методов
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"北京","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}            
             */
            out.println("Сначала сортировка, затем обход");
            list1.stream().forEachOrdered(o -> {
                out.println(new Gson().toJson(o));
            });
/*
Сначала сортировка, затем обход
{"height":170,"weight":50,"identifier":"3","address":"北京","birthday":"Mar 1, 1983 12:00:00 AM","hobbies":["吃飯","上網"],"sex":"X"}
{"height":170,"weight":50,"identifier":"2","address":"北京","birthday":"Feb 1, 1982 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":165,"weight":50,"identifier":"1","address":"北京","birthday":"Jan 1, 1981 12:00:00 AM","hobbies":["吃飯","逛街"],"sex":"X"}
{"height":150,"weight":35,"identifier":"4","address":"北京","birthday":"Jan 1, 1984 12:00:00 AM","hobbies":["吃飯","看電影"],"sex":"Male"}
 */
            out.println("peek*2");
            List<Integer> l = Stream.iterate(0, (Integer n) -> n + 1) //Генерирует арифметическую прогрессию, увеличивающуюся на 1 каждый раз
                    .peek(n -> out.println("number generated:" + n))
                    .filter(n -> (n % 2 == 0))
                    .peek(n -> out.println("Even number filter passed for" + n)) //Продолжает вывод при встрече четных чисел
                    .limit(5).collect(Collectors.toList());
            out.println(new Gson().toJson(l));
            //Можно увидеть, что при генерации до 9 эта операция завершилась, потому что был установлен limit
            /*
peek*2
number generated:0
Even number filter passed for0
number generated:1
number generated:2
Even number filter passed for2
number generated:3
number generated:4
Even number filter passed for4
number generated:5
number generated:6
Even number filter passed for6
number generated:7
number generated:8
Even number filter passed for8
[0,2,4,6,8]            
             */
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.println("done");
        }
    }


}

// 

Ссылки:

  1. Java 8 中的 Streams API 详解
  2. Introduction to the Java 8 Date/Time API
  3. Convert java.time.LocalDate into java.util.Date type
  4. Java 8之Stream API
  5. Comparator.comparing(…) throwing non-static reference exception while taking String::compareTo
  6. 采用java8 lambda表达式 实现java list 交集/并集/差集/去重并集
  7. 详解Java中的clone方法 – 原型模式
  8. Collection to stream to a new collection
  9. Java parallel stream用法
  10. [Java 8 – How to ‘peek’ into a running Stream Stream.peek method tutorial with examples](https://www.javabrahman.com/java-8/java-8-how-to-peek-into-a-running-stream-peek-method-tutorial-with-examples/)
  11. JDK8函数式接口Function、Consumer、Predicate、Supplier