美文网首页
lambda表达式(2)

lambda表达式(2)

作者: YNZXGWZM | 来源:发表于2018-08-02 17:35 被阅读0次
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)

      本文链接:https://www.haomeiwen.com/subject/wzysvftx.html