Stream用法
准备Employee类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private String name;
private Integer age;
private Double salary;
private Status status;
}
Status 枚举类
public enum Status{
FREE,
BUSY,
VOCATION;
}
构造一个集合
List<Employee> list = Arrays.asList(
new Employee("张三", 12, 111.11, Status.FREE),
new Employee("李四", 32, 222.22, Status.BUSY),
new Employee("王五", 22, 333.33, Status.FREE),
new Employee("赵六", 52, 444.44, Status.VOCATION),
new Employee("赵六", 52, 444.44, Status.VOCATION)
);
一、筛选与切片
1.filter
@Test
public void test1(){
list.stream().filter((e) -> e.getAge() == 12).forEach(System.out::println);
}
2.limit
@Test
public void test2(){
list.stream().limit(2).forEach(System.out::println);
}
3.skip
@Test
public void test3(){
list.stream().skip(2).forEach(System.out::println);
}
4.distinct
@Test
public void test4(){
list.stream().distinct().forEach(System.out::println);
}
二、映射
1.map
@Test
public void test1(){
List<String> strs=Arrays.asList("a","b","c","d");
strs.stream().map(e->e.toUpperCase()).forEach(System.out::println);
}
三、排序
1.sorted
@Test
public void test1(){
List<String> strs=Arrays.asList("b","c","a","d");
strs.stream().sorted().forEach(System.out::println);
}
2.sorted(Comparator com)
@Test
public void test1(){
list.stream().sorted((e1,e2)->Double.compare(e1.getSalary(),e2.getSalary())).forEach(System.out::println);
}
三、查找与匹配
1.allMatch-检查是否匹配所有元素
@Test
public void test1(){
boolean b = list.stream().allMatch(e -> e.getAge()>13);
System.out.println(b);
}
2.anyMatch-检查是否至少匹配一个元素
@Test
public void test1(){
boolean b = list.stream().anyMatch(e -> e.getAge()>13);
System.out.println(b);
}
3.noneMatch-检查是否没有匹配的元素
@Test
public void test1(){
boolean b = list.stream().noneMatch(e -> e.getAge()==13);
System.out.println(b);
}
4.findFirst–返回第一个元素
@Test
public void test1(){
Optional<Employee> first = list.stream().findFirst();
System.out.println(first.get());
}
5.findAny–返回当前流中的任意元素
@Test
public void test1(){
Optional<Employee> first = list.parallelStream().findAny();
System.out.println(first.get());
}
6.count–返回流中的总个数
@Test
public void test1(){
long count = list.stream().count();
System.out.println(count);
}
7.max–返回最大值
@Test
public void test1(){
Optional<Employee> max = list.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(max.get());
}
8.min–返回最小值
@Test
public void test1(){
Optional<Employee> max = list.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(max.get());
}
四、规约与收集操作
1.规约
//归约 reduce(BinaryOperator<T> accumulator);
//reduce(T identity, BinaryOperator<T> accumulator);
@Test
public void test(){
Optional<Double> reduce = list.stream().map(Employee::getSalary).reduce(Double::sum);
System.out.println(reduce.get());
List<Integer> list=Arrays.asList(1,2,3,4,5);
Integer reduce1 = list.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce1);
}
2.收集
//收集 collect
@Test
public void test1(){
List<String> collect = list.stream().map(Employee::getName).collect(Collectors.toList());
collect.forEach(System.out::println);
Set<String> collect1 = list.stream().map(Employee::getName).collect(Collectors.toSet());
collect1.forEach(System.out::println);
}
@Test
public void test2(){
//总和
Double collect = list.stream().collect(Collectors.summingDouble(Employee::getSalary));
System.out.println("工资总和:"+collect);
//平均值
Double collect1 = list.stream().collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println("工资平均值:"+collect1);
//总数
long count = list.stream().count();
System.out.println("总数:"+count);
//最大值
Optional<Employee> collect2 = list.stream().collect(Collectors.maxBy((e1, e2) -> e1.getAge() - e2.getAge()));
Optional<Integer> collect4 = list.stream().map(Employee::getAge).collect(Collectors.maxBy(Integer::compareTo));
System.out.println("最大值:"+collect2.get());
//最小值
Optional<Employee> collect3 = list.stream().collect(Collectors.minBy((e1, e2) -> e1.getAge() - e2.getAge()));
Optional<Integer> collect5 = list.stream().map(Employee::getAge).collect(Collectors.minBy(Integer::compareTo));
System.out.println("最小值:"+collect3.get());
}
DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(Employee::getAge));
System.out.println("平均值:"+collect.getAverage());
System.out.println("最大值:"+collect.getMax());
3.分组
@Test
public void test3(){
Map<Status, List<Employee>> collect = list.stream().collect(Collectors.groupingBy(Employee::getStatus));
Set<Map.Entry<Status, List<Employee>>> entries = collect.entrySet();
for (Map.Entry<Status, List<Employee>> entry : entries) {
Status key = entry.getKey();
List<Employee> value = entry.getValue();
System.out.println("key:"+key+"\t value:"+value);
}
}
//多级分组
@Test
public void test4(){
Map<Status, Map<String, List<Employee>>> collect = list.stream()
.collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
if (((Employee) e).getAge() < 20) {
return "青年";
} else if (((Employee) e).getAge() < 50) {
return "中年";
} else {
return "老年";
}
})));
Set<Map.Entry<Status, Map<String, List<Employee>>>> entries = collect.entrySet();
for (Map.Entry<Status, Map<String, List<Employee>>> entry : entries) {
Set<Map.Entry<String, List<Employee>>> entries1 = entry.getValue().entrySet();
for (Map.Entry<String, List<Employee>> stringListEntry : entries1) {
System.out.println("key:"+entry.getKey()+"\t value:{"+"key:"+stringListEntry.getKey()+"\t value:"+stringListEntry.getValue()+"}");
}
}
}
4.分区
@Test
public void test5(){
Map<Boolean, List<Employee>> collect = list.stream().collect(Collectors.partitioningBy((e) -> e.getAge() > 20));
System.out.println(collect);
}
5.连接字符串
@Test
public void test7(){
String collect = list.stream().map(Employee::getName).collect(Collectors.joining(",","*","*"));
System.out.println(collect);
}
}