Stream的使用
Stream的使用
Stream 流主要用来对集合数据进行过滤,映射,排序。也可用于数组
Stream 可以用来代替for循环对集合数据进行遍历处理,可以极大地简化代码
用法
常用的一个是过滤器filter,添加过滤条件
filter函数输入是谓词Predicate的类型
另一个是映射器map
peek可以提供map+return的功能
flatMap可以提供展开Stream流嵌套Stream流的功能
实践
package com.zimuge.stream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
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);
}