forked from BinaryBall/java-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTest.java
More file actions
59 lines (51 loc) · 2.01 KB
/
StreamTest.java
File metadata and controls
59 lines (51 loc) · 2.01 KB
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
package com.jamal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
/**
* java8
* 2019/10/17 16:41
*
* @author 曾小辉
**/
public class Stream {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
personList.add(new Person(20, "张三"));
personList.add(new Person(30, "张为为"));
personList.add(new Person(25, "张无畏"));
personList.add(new Person(29, "张斯"));
personList.add(new Person(21, "张逼"));
personList.add(new Person(20, "里斯"));
personList.add(new Person(28, "科尔"));
personList.add(new Person(25, "莫雷"));
personList.add(new Person(40, "校花"));
// personList.stream().forEach((person)-> System.out.println(person.getName()));
// personList.stream().filter((person) -> {
// System.out.println("filter:"+person.getName());
// return person.getAge()>22;
// })
// .map((person)->{
// System.out.println("map:"+person.getName());
// return person;
// })
// .sorted(Comparator.comparing(Person::getAge))
// .forEach(person -> System.out.println("姓名:"+person.getName()+" 年龄:"+person.getAge()));
// java.util.stream.Stream<Person> s = personList.stream();
// s.forEach(System.out::println);
// s.forEach(System.out::println);
// List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
// numbers.stream()
// .filter(i -> i%2==0)
// .distinct()
// .forEach(System.out::println);
List<Integer> list = personList.stream()
.map(Person::getName)
.map(String::length)
.collect(toList());
System.out.println(list);
}
}