网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容 知识问答

java jdk1.8新特性Stream API

时间:2024-11-03 17:50:18

1、创建Streampackage com.stream.api1;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Stream;import org.junit.Test;/*** 一 、 Stream 的三个操作步骤 1、创建Stream 2、中级操作 3、终止操作(终端操作)** @author Administrator**/public class TestStreamAPI1 { // 创建Stream @Test public void test1() { // 1、可以通过Collection 系列集合提供的stream()或parallelStream() List<String> list = new ArrayList<String>(); Stream<String> stream1 = list.stream(); // 2、通过Arrays中的静态方法stream()获取数组流 Employee[] ems = new Employee[10]; Stream<Employee> stream2 = Arrays.stream(ems); // 3、通过Stream类中的静态方法of() Stream<String> stream3 = Stream.of("aa", "bb", "cc", "dd"); // 4、创建无限流 Stream<Integer> stream4 = Stream.iterate(0, x -> x + 2); // stream4.forEach(System.out::println); stream4.limit(10).forEach(System.out::println); // 生成 Stream.generate(() -> Math.random()).limit(5).forEach(System.out::println); }}

java jdk1.8新特性Stream APIjava jdk1.8新特性Stream APIjava jdk1.8新特性Stream API

5、Stream查找与匹配package com.stream.api5;import java.util.Arrays;import java.util.List;import java.util.Optional;import org.junit.Test;import com.stream.api1.Employee;import com.stream.api1.Employee.Status;public class TestStreamAPI5 { List<Employee> employees = Arrays.asList(new Employee("张三", 12, 1200.99, Status.BUSY), new Employee("小明", 15, 4500.99, Status.BUSY), new Employee("小丽", 16, 5500.99, Status.BUSY), new Employee("王二", 32, 1100.99, Status.FREE), new Employee("二虎", 22, 9825.99, Status.FREE), new Employee("李静", 18, 4502.99, Status.FREE), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION)); /** * 查找与匹配 allMatch —— 检查是否平匹配所有元素 anyMatch —— 检查是否至少匹配一个元素 noneMatch —— * 检查是否没有匹配的元素 findFirst —— 返回第一个元素 findAny —— 返回当前流中的任意元素 count —— 返回流中元素的总个数 * max —— 返回流中最大值 min —— 返回流中最小值 */ @Test public void test1() { boolean b1 = employees.stream().allMatch(e -> e.getStatus().equals(Status.BUSY)); System.out.println(b1); boolean b2 = employees.stream().anyMatch((e) -> e.getStatus().equals(Status.BUSY)); System.out.println(b2); boolean b3 = employees.stream().noneMatch((e) -> e.getStatus().equals(Status.BUSY)); System.out.println(b3); Optional<Employee> op1 = employees.stream().sorted((e1, e2) -> -Double.compare(e1.getSalary(), e2.getSalary())) .findFirst(); System.out.println(op1.get()); Optional<Employee> op2 = employees.stream().filter((e) -> e.getStatus().equals(Status.FREE)).findAny(); System.out.println(op2.get()); Optional<Employee> op3 = employees.parallelStream().filter((e) -> e.getStatus().equals(Status.FREE)).findAny(); System.out.println(op3.get()); } @Test public void test2() { Long count = employees.stream().count(); System.out.println(count); Optional<Employee> op1 = employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())); System.out.println(op1.get()); Optional<Double> op2 = employees.stream().map(Employee::getSalary).max(Double::compare); System.out.println(op2.get()); }}

java jdk1.8新特性Stream API

6、Stream约与收集归package com.stream.api6;import java.util.Arrays;i罪焐芡拂mport java.util.DoubleSummaryStatistics;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Optional;import java.util.Set;import java.util.stream.Collectors;import org.junit.Test;import com.stream.api1.Employee;import com.stream.api1.Employee.Status;public class TestStreamAPI6 { List<Employee> employees = Arrays.asList(new Employee("张三", 12, 1200.99, Status.BUSY), new Employee("小明", 15, 4500.99, Status.BUSY), new Employee("小丽", 16, 5500.99, Status.BUSY), new Employee("王二", 32, 1100.99, Status.FREE), new Employee("二虎", 22, 9825.99, Status.FREE), new Employee("李静", 18, 4502.99, Status.FREE), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION)); /** * 归约 reduce(T identity,BinaryOperator)/reduce(BinaryOperator) —— * 可以将流中元素反复结合起来,得到一个值 */ @Test public void test1() { 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> op1 = employees.stream().map(Employee::getSalary).reduce(Double::sum); System.out.println(op1.get()); } /** * 收集 collect —— 将流转换为其它形式,接收一个Collector接口的实现,用于给Stream中元素做汇总的方法 */ @Test public void test2() { 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 test3() { // 总数 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); System.out.println("-----------------------"); // 总和 Double sum = employees.stream().collect(Collectors.summingDouble(Employee::getSalary)); System.out.println(sum); System.out.println("-----------------------"); // 最大值 Optional<Employee> max = employees.stream() .collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))); System.out.println(max.get()); System.out.println("-----------------------"); // 最大值 Optional<Employee> min = employees.stream() .collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))); System.out.println(min.get()); } // 分组 @Test public void test4() { Map<Status, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getStatus)); System.out.println(map); } // 多级分组 @Test public void test5() { Map<Status, Map<String, List<Employee>>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> { if (e.getAge() < 16) { return "青年"; } else if (e.getAge() < 18) { return "中年"; } else { return "老年"; } }))); System.out.println(map); } // 分区 @Test public void test6() { Map<Boolean, List<Employee>> map = employees.stream() .collect(Collectors.partitioningBy(e -> e.getSalary() > 5000)); System.out.println(map); } @Test public void test7() { DoubleSummaryStatistics collect = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary)); System.out.println(collect.getAverage()); System.out.println(collect.getCount()); System.out.println(collect.getMax()); System.out.println(collect.getMin()); System.out.println(collect.getSum()); } @Test public void test8() { String str1 = employees.stream().map(Employee::getName).collect(Collectors.joining()); System.out.println(str1); System.out.println("---------------------------------"); String str2 = employees.stream().map(Employee::getName).collect(Collectors.joining(",")); System.out.println(str2); System.out.println("---------------------------------"); String str3 = employees.stream().map(Employee::getName).collect(Collectors.joining(",", "===", "===")); System.out.println(str3); }}

java jdk1.8新特性Stream APIjava jdk1.8新特性Stream API

7、Stream约练习

© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com