java8-求最小值(8中方法)

List employees = Arrays.asList(
        new Employee(101, "张三", 18, 9999.99),
        new Employee(102, "李四", 59, 6666.66),
        new Employee(103, "王五", 28, 3333.33),
        new Employee(104, "赵六", 8, 7777.77),
        new Employee(105, "田七", 38, 5555.55)
);

 

    /**
     * 注意:参数列表
     * 1.实现的接口中的方法的参数列表和返回值要与
     * 方法引用中的实例方法的参数列表这返回值一致;
     * 2.lamdba表达式不会去实现它本身的方法,这个方法其使用->代替
     * 只关系参数列表和方法体
     * 3.方法或者变量属于类方法或者实例方法
     */

    @Test
    public void test1() {
        Consumer consumer = (x) -> System.out.println(x); //创建实例
        Consumer consumer1 = System.out::println;   //创建实例
    }

    @Test
    public void test3() {
        Comparator comparator = (a1, a2) -> Integer.compare(a1, a2);
        /**
         *相当于把a1和a2传到compare中去
         */
        Comparator comparator1 = Integer::compare;
    }

    //reduce   ---->将流中的元素反复结合起来,得到一个值

    /**
     * T reduce(T identity, BinaryOperator accumulator);
     * 

* Optional reduce(BinaryOperator accumulator); */ @Test public void test4() { List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); //这个其实调的是BizFunction中的apply Integer reduce = list.stream().reduce(0, (x, y) -> x + y); //将x和y作为参数传到sum中 Integer reduce1 = list.stream().reduce(0, Integer::sum); //求其最大值 Integer result = list.stream() .reduce(BinaryOperator .maxBy(Integer::compare)).orElseGet(() -> { return 0; }); System.out.println(reduce); System.out.println(reduce1); System.out.println(result); } //公司中员工工资的总和 //lamdba表达式可以用方法引用代替,这个二者是分不开的 //传参和不传参 //map和reduce @Test public void test5() { Double aDouble = employees.stream() .map(e -> e.getSalary()).reduce((x, y) -> x + y) .get(); // String.format("d%", aDouble); Double aDouble1 = employees.stream() .map(Employee::getSalary) .reduce(Double::sum) .get(); //求最大值 Double aDouble2 = employees.stream() .map(Employee::getSalary) .reduce(BinaryOperator .maxBy(Double::compareTo)).get(); System.out.println(aDouble); System.out.println(aDouble1); System.out.println(aDouble2); } //Supplier--供给,不仅供给数据,任何容器也是供给 @Test public void test() { HashSet result = employees.stream() .collect(Collectors.toCollection(HashSet::new)); System.out.println(result); } //counting @Test public void test7() { Long collect = employees.stream() .collect(Collectors.counting()); System.out.println(collect); } //求平均值 /** * 有lamdba表达式的地方就有方法引用,因为这样才能理解到外 */ @Test public void test8() { Double collect = employees.stream() .collect(Collectors.averagingDouble(Employee::getSalary)); Double collect1 = employees.stream().collect(Collectors.averagingDouble(o -> o.getSalary())); System.out.println(collect); System.out.println(collect1); } /** * sum */ @Test public void test9() { Double sumResult = employees.stream().collect(Collectors.summingDouble(Employee::getSalary)); System.out.println(sumResult); } /** * java8求最小值的几种写法 */ @Test public void test10() { //1. Optional result = employees.stream().collect(Collectors.minBy(((o1, o2) -> (int) (o1.getSalary() - o2.getSalary())))); Employee employee = result.get(); System.out.println(employee.getSalary()); //2. Employee employee2 = employees.stream() .collect(Collectors .minBy(Comparator. comparingDouble((employee1) -> employee1.getSalary()))).get(); System.out.println(employee2.getSalary()); //3. Employee employee3 = employees.stream().min(Comparator. comparingDouble((employee1) -> employee1.getSalary())).get(); //4. Employee employee4 = employees.stream() .collect(Collectors.minBy(Comparator.comparing(Employee::getSalary))).get(); //5.也可以根据map+reduce的方法进行 System.out.println(employee.getSalary()); System.out.println(employee2.getSalary()); System.out.println(employee3.getSalary()); System.out.println(employee4.getSalary()); }

你可能感兴趣的:(java8-求最小值(8中方法))