/**
*
* 終止操作 歸約與收集:
*
* 1、歸約 reduce(T identity, BinaryOperator) / reduce(BinaryOperator)
* ——可以將流中元素反覆結合起來,得到一個值。
*
*
* 2、收集 collect——將流轉換爲其他形式。接收一個 Collector接口的實現,用於給Stream中元素做彙總的方法
*
* 3、分組、多級分組
*
* 4、拼接
*
*/private List<Employee> emps = Arrays.asList(
new Employee("James", 36, 100000.0, Status.BUSY),
new Employee("Jordan", 25, 8000.0, Status.FREE), new Employee("Howard", 56, 5000.0, Status.VACATION),
new Employee("Howard", 56, 5000.0, Status.FREE), new Employee("Howard", 56, 5000.0, Status.VACATION),
new Employee("Irving", 15, 3000.0, Status.FREE));
一、歸約操作
/***
* reduce(T identity, BinaryOperator) / reduce(BinaryOperator)
* ——可以將流中元素反覆結合起來,得到一個值。
*/
@Test
public void test() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// 這裏有起始值0,所以返回值不會進行包裝
int reduce = list.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce);
}
/***
* reduce(T identity, BinaryOperator) / reduce(BinaryOperator)
* ——可以將流中元素反覆結合起來,得到一個值。
*/
@Test
public void test2() {
// 這裏無起始值,所以返回值不確定,需要進行包裝
Optional<Double> reduce = emps.stream().map(Employee::getSalary).reduce(Double::sum);
System.out.println(reduce.get());
}
二、收集操作
/***
* collect——將流轉換爲其他形式。接收一個 Collector接口的實現,用於給Stream中元素做彙總的方法
*/
@Test
public void test3() {
List<String> collect = emps.stream().map(Employee::getName).collect(Collectors.toList());
collect.forEach(System.out::println);
}
/***
* collect——將流轉換爲其他形式。接收一個 Collector接口的實現,用於給Stream中元素做彙總的方法
*/
@Test
public void test4() {
Set<String> collect = emps.stream().map(Employee::getName).collect(Collectors.toSet());
collect.forEach(System.out::println);
}
/***
* collect——將流轉換爲其他形式。接收一個 Collector接口的實現,用於給Stream中元素做彙總的方法
*/
@Test
public void test5() {
HashSet<String> collect = emps.stream().map(Employee::getName)
// 收集到HashSet中,LinkedHashSet類似
.collect(Collectors.toCollection(HashSet::new));
collect.forEach(System.out::println);
}
/***
* 收集
*/
@Test
public void test6() {
// 求集合個數
long collect = emps.stream().collect(Collectors.counting());
System.out.println(collect);
// 求工資平均值
Double collect2 = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println(collect2);
// 求工資總和
Double collect3 = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
System.out.println(collect3);
// 求工資最大員工
Optional<Employee> collect4 = emps.stream()
.collect(Collectors.maxBy((x, y) -> Double.compare(x.getSalary(), y.getSalary())));
System.out.println(collect4.get());
// 求工資最小值
Optional<Double> collect5 = emps.stream().map(Employee::getSalary).collect(Collectors.minBy(Double::compare));
System.out.println(collect5.get());
}
三、分組、分區、拼接操作
/***
* 分組
*/
@Test
public void test7() {
Map<Status, List<Employee>> collect = emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
System.out.println(collect);
}
/***
* 多級分組
*/
@Test
public void test8() {
Map<Status, Map<String, List<Employee>>> collect = emps.stream()
// //可進行多級分組
.collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy(t -> {
if (t.getAge() <= 35) {
return "青年";
} else if (t.getAge() <= 50) {
return "中年";
} else {
return "老年";
}
})));
System.out.println(collect);
}
/***
* 分區
*/
@Test
public void test9() {
Map<Boolean, List<Employee>> map = emps.stream()
//可進行多級分區
.collect(Collectors.partitioningBy(t -> t.getSalary() > 8000));
System.out.println(map);
}
/***
* 統計
*/
@Test
public void test10() {
DoubleSummaryStatistics collect = emps.stream()
.collect(Collectors.summarizingDouble(Employee :: getSalary));
System.out.println(collect.getAverage());
System.out.println(collect.getCount());
System.out.println(collect.getMax());
System.out.println(collect.getMin());
System.out.println(collect.getSum());
}
/***
* 拼接
*
* ===James,Jordan,Howard,Howard,Howard,Irving===
*/
@Test
public void test11() {
String collect = emps.stream()
.map(Employee :: getName)
.collect(Collectors.joining(",","===","==="));
System.out.println(collect);
}