zoukankan      html  css  js  c++  java
  • stream — 查找与匹配(四)

    查找与匹配

        List<Employee> employees = Arrays.asList(//
                new Employee(20, "张三", 5000.35, Status.FREE), //
                new Employee(40, "李四", 6500.63, Status.BUSY), //
                new Employee(30, "王五", 4000.93, Status.FREE), //
                new Employee(50, "赵六", 9005.36, Status.BUSY), //
                new Employee(10, "马七", 1050.93, Status.VOCATION), //
                new Employee(20, "朱八", 3000.73, Status.BUSY)//
        );
    
        @Test
        public void test1() {
    
            // 1、allMatch—检查是否匹配所有元素
            boolean allMatch = employees.stream().allMatch((e) -> e.getStatus().equals(Status.FREE));
            System.out.println(allMatch);
    
            // 2、anyMatch—检查是否至少匹配一个元素
            boolean anyMatch = employees.stream().anyMatch((e) -> e.getStatus().equals(Status.FREE));
            System.out.println(anyMatch);
    
            // 3、noneMatch—检查是否没有匹配所有元素
            boolean noneMatch = employees.stream().noneMatch((e) -> e.getStatus().equals(Status.FREE));
            System.out.println(noneMatch);
    
            // 4、findFirst—返回第一个元素
            Optional<Employee> findFirst = employees.stream()
                    .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())).findFirst();
            System.out.println(findFirst.get());
    
            // 5、findAny—返回当前流中的任意元素
            Optional<Employee> findAny = employees.stream().filter((e) -> e.getStatus().equals(Status.FREE)).findAny();
            System.out.println(findAny.get());
    
            // 6、count—返回流中元素的总个数
            long count = employees.stream().count();
            System.out.println(count);
    
            // 7、max—返回流中最大值
            Optional<Employee> max = employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
            System.out.println(max.get());
    
            // 8、min一返回流中最小值
            Optional<Double> min = employees.stream().map(Employee::getSalary).min(Double::compare);
            System.out.println(min.get());
        }
  • 相关阅读:
    [HNOI2008]神奇的国度(最大势算法)
    学习笔记——prufer序列
    [NOIP模拟题]chess(最短路计数)
    2019暑假图论总结
    [NOIP2016]天天爱跑步(桶)
    [NOIP2012]疫情控制(贪心)
    [NOIP2016]蚯蚓(单调性乱搞)
    暑假考试题4:星际旅行(欧拉路)
    暑假考试题3:jigsaw 黄金拼图(乱搞)
    暑假考试题3:baritone 上低音号与星星(链表+矩形统计)
  • 原文地址:https://www.cnblogs.com/zhanh247/p/11854445.html
Copyright © 2011-2022 走看看