package com.mc.day1.lambda;
import com.mc.Employee;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
/**
* =================规约与收集==================
*/
public class Lambda2 {
public List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99, Employee.Status.FREE1),
new Employee(102, "李四", 59, 6666.66, Employee.Status.BUSSY),
new Employee(103, "王五", 28, 3333.33, Employee.Status.FREE1),
new Employee(104, "赵六", 28, 7777.77, Employee.Status.VOCATION),
new Employee(105, "田七", 38, 5555.55, Employee.Status.BUSSY),
new Employee(105, "田七", 110, 5555.55, Employee.Status.BUSSY)
);
@Test
public void test1() {
System.out.println("===================================");
emps.stream().sorted((x1, x2) -> {
if (x1.getAge() == x2.getAge()) {
return x1.getName().compareTo(x2.getName());
}
return Integer.compare(x1.getAge(), x2.getAge());
}).map(x -> x.getName()).forEach(x -> System.out.println(x));
System.out.println("===================================");
boolean b = emps.stream().allMatch((e) -> (e.getStatus().equals(Employee.Status.BUSSY)));
System.out.println(b);
System.out.println("===================================");
boolean b1 = emps.stream().anyMatch((e) -> (e.getStatus().equals(Employee.Status.BUSSY)));
System.out.println(b1);
System.out.println("===================================");
boolean b2 = emps.stream().noneMatch((e) -> (e.getStatus().equals(Employee.Status.BUSSY)));
System.out.println(b2);
System.out.println("===================================");
Optional<Employee> firstResult = emps.stream().sorted(Comparator.comparingDouble(Employee::getSalary)).findFirst();
Employee employee = firstResult.get();
System.out.println(employee);
System.out.println("===================================");
long count = emps.stream().filter(x -> x.getSalary() > 6000).count();
System.out.println(count);
System.out.println("===================================");
Optional<Employee> max = emps.stream().max(Comparator.comparingDouble(Employee::getSalary));
Employee employee1 = max.get();
System.out.println(employee1);
System.out.println("===================================");
Optional<Double> min = emps.stream().map(Employee::getSalary).min(Double::compare);
Optional<Double> min1 = emps.stream().map(Employee::getSalary).min((x1, x2) -> Double.compare(x1, x2));
System.out.println("=================规约与收集==================");
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer reduce = list.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce);
System.out.println("===================================");
System.out.println("工资有可能为空,所有框架封装成Optional");
Optional<Double> reduce1 = emps.stream().map(Employee::getSalary).reduce(Double::sum);
System.out.println(reduce1);
System.out.println("当前员工所有的名字收集到一个集合中");
emps.stream().map(Employee::getName).collect(Collectors.toList()).forEach(System.out::println);
System.out.println("list集合元素去重:注意要实现 实体VO的hashcode方法和equals方法");
emps.stream().distinct().collect(Collectors.toList()).forEach(x -> System.out.println(x.getName()));
System.out.println("拿到一个名字的集合去重, 放入set去重");
emps.stream().map(Employee::getName).collect(Collectors.toSet()).stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);
System.out.println("拿到一个名字的集合去重, 放入set去重 第二种方法");
emps.stream().map(Employee::getName).distinct().collect(Collectors.toList()).forEach(System.out::println);
System.out.println("拿到一个名字的集合去重, 放入set去重 第三种方法");
emps.stream().map(Employee::getName).distinct().collect(Collectors.toList()).stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);
System.out.println("拿到一个集合,通过键值对 ,放入map集合");
Map<String, Employee> employeeMap = emps.stream().collect(Collectors.toMap(Employee::getName, x -> x, (x1, x2) -> x2));
System.out.println("拿到一个名字的集合,放入linkedList");
LinkedList<String> collect = emps.stream().map(Employee::getName).collect(Collectors.toCollection(() -> new LinkedList<>()));
LinkedList<String> collect1 = emps.stream().map(Employee::getName).collect(Collectors.toCollection(LinkedList::new));
HashSet<String> collect2 = emps.stream().map(Employee::getName).collect(Collectors.toCollection(HashSet::new));
System.out.println("工资的平均值");
Double collect3 = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println("取出工资最大的员工");
Optional<Employee> collect4 = emps.stream().collect(Collectors.maxBy((x1, x2) -> Double.compare(x1.getSalary(), x2.getSalary())));
System.out.println(collect4.get());
System.out.println("员工按照状态分组");
Map<Employee.Status, List<Employee>> collect5 = emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
System.out.println("员工多级分组,按照状态分组,分组完成后,组内按照年龄分组");
Map<Employee.Status, Map<String, List<Employee>>> collect6 = emps.stream().collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
if (e.getAge() < 30) {
return "青年";
} else {
return "老年";
}
})));
System.out.println("分片操作");
Map<Boolean, List<Employee>> collect7 = emps.stream().collect(Collectors.partitioningBy(x -> x.getName().equals("田七")));
System.out.println(collect7);
System.out.println("对集合特定的属性进行拼接");
String collect8 = emps.stream().map(Employee::getName).collect(Collectors.joining());
System.out.println(collect8);
System.out.println("=================规约与收集==================");
}
}
lambda表达式(2)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 上一篇文章:基于Java8的Lambda表达式(1)本篇继续讲几个常用的写法: 01.代码 02.说明 filte...
- 初识Lambda表达式 上一篇中,我们介绍了行为参数化的编程模式,它能够轻松地适应不断变化的需求。在介绍这一模式时...