-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStreamAPI3.java
More file actions
58 lines (50 loc) · 1.65 KB
/
TestStreamAPI3.java
File metadata and controls
58 lines (50 loc) · 1.65 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
package Java8Study.stream3;
import Java8Study.lambda1.Employee;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class TestStreamAPI3 {
/**
* 给定一个数字列表,返回由每个数字平方组成的列表
* 1,2,3,4 -> 1,4,9,16
*/
@Test
public void test1(){
//map做法
int[] arr ={1,2,3,4};
Arrays.stream(arr).map(x -> x*x).forEach(System.out::println);
}
/**
* 用map和reduce得到流中有多少employee
*/
List<Employee> employees = Arrays.asList(
new Employee("jerry",18,9999.1, Employee.Status.FREE),
new Employee("tom",23,1000,Employee.Status.BUSY),
new Employee("amy",26,1234, Employee.Status.VACATION),
new Employee("pony",45,12442.1, Employee.Status.FREE),
new Employee("tony",50,1232, Employee.Status.VACATION)
);
/**
* 用map和reduce得到流中有多少employee
*/
@Test
public void test2(){
Optional<Integer> sum = employees.stream().map(e -> 1).reduce(Integer::sum);
System.out.println(sum);
}
/**
* 找出工作状态为 FREE,且年龄最大的
*/
@Test
public void test3(){
Optional<Employee> freeOldest = employees.stream()
.filter( e -> e.getStatus().equals(Employee.Status.FREE))
.max(Comparator.comparingInt(Employee::getAge));
System.out.println("工作状态为 FREE,且年龄最大的 : "+ freeOldest.get());
}
/**
* 找出
*/
}