Stream流的集合元素归约
Stream流的集合元素归约
什么是归约?
归约即将集合的所有元素缩减为一个值,例如求均值、求和等
stream中使用reduce函数实现
字母哥最后一节视频讲的很好,推荐看视频
https://www.bilibili.com/video/BV1sE411P7C1?p=10&vd_source=3c88fb7dae36f53e6a15081fb7cf9ff8
实践
package com.zimuge.stream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
/**
* 功能描述
*
* @since 2022-06-13
*/
public class StreamTest {
@Test
public void test1() {
// list 流过滤
List<String> strings = Arrays.asList("zhangsan", "lisi", "wangwu", "zhouliu");
List<String> sorted = strings.stream().filter(str -> str.startsWith("z")).map(String::toUpperCase).sorted().collect(Collectors.toList());
System.out.println(sorted);
// Array 数组流过滤
String[] str1 = {"zhangsan", "lisi", "wangwu", "zhouliu"};
List<String> sorted1 = Stream.of(str1).filter(str -> str.endsWith("u")).map(String::toLowerCase).sorted().collect(Collectors.toList());
System.out.println(sorted1);
// Set 流过滤
HashSet<String> strings1 = new HashSet<>(strings);
Set<String> z = strings1.stream().filter(str -> str.startsWith("z")).map(String::toUpperCase).sorted().collect(Collectors.toSet());
System.out.println(z);
}
@Test
public void test2() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
// List<Employee> f = list.stream()
// .filter(e -> e.getAge() > 35 || e.getGender().equals("M"))
// .collect(Collectors.toList());
// 两个实现都可以,下面的实现谓词可以复用
List<Employee> f = list.stream()
.filter(Employee.ageGreaterThan35.or(Employee.genderM))
.collect(Collectors.toList());
System.out.println(f);
}
@Test
public void test3() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
List<Employee> m = list.stream()
.filter(v -> v.getAge() > 25)
.map(e -> {
e.setAge(e.getAge() + 1);
e.setGender(e.getGender().equals("M") ? "Male" : "Female");
return e;
})
.collect(Collectors.toList());
System.out.println(m);
// 与上面map+return一样
List<Employee> m1 = list.stream()
.filter(v -> v.getAge() > 25)
.peek(e -> {
e.setAge(e.getAge() + 1);
e.setGender(e.getGender().equals("M") ? "Male" : "Female");
})
.collect(Collectors.toList());
System.out.println(m1);
}
@Test
public void test4() {
List<String> list = Arrays.asList("Hello", "World");
list.stream()
.map(e -> e.split(""))
.forEach(System.out::println);
list.stream()
.map(e -> Arrays.stream(e.split("")))
.forEach(System.out::println);
list.stream()
.flatMap(e -> Arrays.stream(e.split("")))
.forEach(System.out::println);
}
/**
* 有状态操作及串并行操作
*/
@Test
public void test5() {
List<String> list = Arrays.asList("Hello", "World", "Aba", "Aba", "haha", "Aba", "dgh", "yeah");
list.stream()
.parallel()
.distinct()
.limit(5)
.skip(2)
.sorted()
.forEach(System.out::println);
}
@Test
public void test6() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
// 都是正序
list.sort(Comparator.comparing(Employee::getGender).thenComparingInt(Employee::getAge));
list.forEach(System.out::println);
// 都是逆序
list.sort(Comparator.comparing(Employee::getGender).thenComparingInt(Employee::getAge).reversed());
list.forEach(System.out::println);
// 一逆一正
list.sort(Comparator.comparing(Employee::getGender).reversed().thenComparingInt(Employee::getAge));
list.forEach(System.out::println);
// 一正一逆
list.sort(Comparator.comparing(Employee::getGender).reversed().thenComparingInt(Employee::getAge).reversed());
list.forEach(System.out::println);
}
@Test
public void test7() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
list.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o2.getAge() - o1.getAge();
}
});
list.forEach(System.out::println);
System.out.println("list = " + list);
}
@Test
public void test8() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
boolean b = list.stream().anyMatch(e -> e.getAge() > 58);
System.out.println(b);
boolean b1 = list.stream().allMatch(e -> e.getAge() > 58);
System.out.println(b1);
Optional<Employee> first = list.stream().filter(e -> e.getAge() > 40).findFirst();
System.out.println(first.get());
// 抛异常
// Optional<Employee> first1 = list.stream().filter(e -> e.getAge() > 80).findFirst();
// System.out.println(first1.get());
Optional<Employee> any = list.stream().filter(e -> e.getAge() > 40).findAny();
System.out.println(any.get());
boolean present = list.stream().filter(e -> e.getAge() > 40).findFirst().isPresent();
System.out.println(present);
list.stream().filter(e -> e.getAge() > 40).findFirst().ifPresent(System.out::println);
}
@Test
public void test9() {
Employee e1 = new Employee(1, 21, "zhangsan", "F");
Employee e2 = new Employee(2, 45, "lisi", "M");
Employee e3 = new Employee(3, 60, "wangwu", "M");
Employee e4 = new Employee(4, 32, "zhouliu", "F");
Employee e5 = new Employee(5, 28, "zhaoqi", "M");
Employee e6 = new Employee(6, 43, "qianba", "F");
List<Employee> list = Arrays.asList(e1, e2, e3, e4, e5, e6);
//求所有员工年龄和最大值
Optional<Integer> reduce = list.stream().map(Employee::getAge).reduce(Integer::sum);
System.out.println(reduce.get());
Optional<Integer> reduce2 = list.stream().map(Employee::getAge).reduce(Integer::max);
System.out.println(reduce2.get());
// 并行流实现
Optional<Integer> reduce3 = list.parallelStream().map(Employee::getAge).reduce(Integer::sum);
System.out.println(reduce3.get());
Integer int1 = list.stream().reduce(0, (total, emp) -> total + emp.getAge(), Integer::sum);
System.out.println(int1);
}
}