zoukankan      html  css  js  c++  java
  • java8-求最小值(8中方法)

    测试:
    List<Employee> 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<String> consumer = (x) -> System.out.println(x); //创建实例
            Consumer<String> consumer1 = System.out::println;   //创建实例
        }
        @Test
        public void test3() {
            Comparator<Integer> comparator = (a1, a2) -> Integer.compare(a1, a2);
            /**
             *相当于把a1和a2传到compare中去
             */
            Comparator<Integer> comparator1 = Integer::compare;
        }
        //reduce   ---->将流中的元素反复结合起来,得到一个值
        /**
         * T reduce(T identity, BinaryOperator<T> accumulator);
         * <p>
         * Optional<T> reduce(BinaryOperator<T> accumulator);
         */
        @Test
        public void test4() {
            List<Integer> 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<Employee> 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<Employee> 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());
        }
    ————————————————
    版权声明:本文为CSDN博主「航海到IT的转变,梦想一直在路上」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/wb_zjp283121/article/details/90782267
  • 相关阅读:
    CPU被挖矿,Redis竟是内鬼!
    图解四种 IO 模型
    用户态和内核态的区别是啥
    关于 RocketMQ ClientID 相同引发的消息堆积的问题
    玩转 ByteBuffer
    RocketMQ Consumer 启动时都干了些啥?
    网络协议之:基于UDP的高速数据传输协议UDT
    dart系列之:安全看我,dart中的安全特性null safety
    JetBrains又出神器啦!Fleet,体验飞一般的感觉
    网络协议之:还在用HTTP代理?弱爆了!快试试SOCKS5
  • 原文地址:https://www.cnblogs.com/moonsoft/p/12514835.html
Copyright © 2011-2022 走看看