List<Employee> employees = Arrays.asList( new Employee("张小", 19, 1999.99, Employee.Status.FREE), new Employee("李而", 18, 2999.99, Employee.Status.BUSY), new Employee("张三", 20, 3999.99, Employee.Status.VOCATION), new Employee("张四", 50, 4999.99, Employee.Status.FREE), new Employee("王五", 38, 5555.55, Employee.Status.BUSY), new Employee("赵六", 30, 6666.66, Employee.Status.VOCATION), new Employee("田七", 21, 7777.77, Employee.Status.FREE), new Employee("不小", 21, 7777.77, Employee.Status.FREE) ); /** * 查找与匹配 * allMatch--s检查是否匹配所有元素 * anyMatch--检查是否至少匹配一个元素 * noneMatch--检查是否没有匹配所有元素 * findFirst--返回当前流中的任意元素 * count--返回流中元素的总个数 * max--返回流中最大值 * min--返回流中最小值 */ @Test public void test1(){ boolean b1 = employees.stream() .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b1); boolean b2 = employees.stream() .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b2); boolean b3 = employees.stream() .noneMatch((e)->e.getStatus().equals(Employee.Status.BUSY)); System.out.println(b3); Optional<Employee> optional = employees.stream() //串行流 .sorted((e1, e2) -> -Double.compare(e1.getSalary(), e2.getSalary())) .findFirst(); //optional.orElse(); //如果有可能为空时给值 System.out.println(optional.get()); Optional<Employee> optional1 = employees.parallelStream() //并行流 .filter((e) -> e.getStatus().equals(Employee.Status.FREE)) .findAny(); System.out.println(optional1.get()); } @Test public void test2(){ long count = employees.stream() .count(); System.out.println(count); Optional<Employee> maxOp = employees.stream() .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(maxOp.get()); Optional<Double> minOp = employees.stream() .map(Employee::getSalary) .min(Double::compare); System.out.println(minOp.get()); } /** * 归约 * reduce(T identity,BinaryOperator) / reduce(BinaryOperator) 可以将流中元素反复结合起来,得到一个值 */ @Test public void test3(){ List<Integer> list=Arrays.asList(1,2,3,4,5,6,7,8,9,10); Integer sum = list.stream() .reduce(0, (x, y) -> x + y); System.out.println(sum); System.out.println("--------------------------"); Optional<Double> optional = employees.stream() .map(Employee::getSalary) .reduce(Double::sum); System.out.println(optional.get()); } /** * 收集 * collect--将流转换为其他形式,接收一个Collector接品的实现,用于给Stream中元素做汇总的方法 */ @Test public void test4(){ List<String> list = employees.stream() .map(Employee::getName) .collect(Collectors.toList()); list.forEach(System.out::println); System.out.println("--------------------------"); Set<String> set = employees.stream() .map(Employee::getName) .collect(Collectors.toSet()); set.forEach(System.out::println); System.out.println("--------------------------"); HashSet<String> hashSet = employees.stream() .map(Employee::getName) .collect(Collectors.toCollection(HashSet::new)); hashSet.forEach(System.out::println); } @Test public void test5(){ //总数 Long count = employees.stream() .collect(Collectors.counting()); System.out.println(count); System.out.println("--------------------------"); //平均值 Double avg = employees.stream() .collect(Collectors.averagingDouble(Employee::getSalary)); System.out.println(avg); //总和 Double sum = employees.stream() .collect(Collectors.summingDouble(Employee::getSalary)); System.out.println(sum); //最大值 Optional<Employee> max = employees.stream() .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))); System.out.println(max.get()); //最小值 Optional<Employee> min = employees.stream() .collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))); System.out.println(min.get()); } //分组 @Test public void test6(){ Map<Employee.Status, List<Employee>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getStatus)); System.out.println(map); } //多极分组 @Test public void test7(){ Map<Double, Map<String, List<Employee>>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getSalary, Collectors.groupingBy((Object e) -> { if (((Employee) e).getAge() <= 35) { return "青年"; } else if (((Employee) e).getAge() <= 50) { return "中年"; } else { return "老年"; } }))); System.out.println(map); } //分片 分区 @Test public void test8(){ Map<Boolean, List<Employee>> map = employees.stream() .collect(Collectors.partitioningBy((e) -> e.getSalary() > 8000)); System.out.println(map); } @Test public void test9(){ DoubleSummaryStatistics collect = employees.stream() .collect(Collectors.summarizingDouble(Employee::getSalary)); System.out.println(collect); System.out.println(collect.getSum()); System.out.println(collect.getAverage()); System.out.println(collect.getMax()); } @Test public void test10(){ String collect = employees.stream() .map(Employee::getName) .collect(Collectors.joining(",","==","==")); System.out.println(collect); }
stream之第三步终止操作(终端操作)
最新推荐文章于 2025-04-09 18:00:50 发布