标签:match mda void stream tst cat equal com count
查找与匹配
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()); }
标签:match mda void stream tst cat equal com count
原文地址:https://www.cnblogs.com/zhanh247/p/11854445.html