ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
- stream流的生成 ``` // 1,校验通过Collection 系列集合提供的stream()或者paralleStream() ArrayList list = new ArrayList(); Stream stream = list.stream(); // 2.通过Arrays的静态方法stream()获取数组流 String[] strs = new String[10]; Stream<String> stream2 = Arrays.stream(strs); // 3.通过Stream类中的静态方法of Stream<String> stream3 = Stream.of("a", "b", "c"); // 4.创建无限流 / 迭代 Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 1); // 生成 Stream<Double> stream5 = Stream.generate(()-> Math.random()); ``` - Stream的中间操作: ``` 1. stream流的筛选过滤去重 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2)); stream.filter(x -> x.getEmployId() > 1) .limit(2) .skip(1) // 需要流中的元素重写hashCode和equals方法 .distinct() .forEach(System.out::println); --------------------------------------------------------------------------------------- 2. 生成新的流,通过map映射 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2)); stream.map(e-> e.getEmployeeName()) .forEach(System.out::println); --------------------------------------------------------------------------------------- 3. stream流自然排序 定制排序 Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2)); stream.sorted((emp1, emp2) -> { if(emp1.getEmployId() > emp2.getEmployId()) { return -1; }else if(emp1.getEmployId() == emp2.getEmployId()) { return 0; }else { return 1; } }) .forEach(System.out::println); ``` - Stream的终止操作: ``` /** * 终止操作 * 查找和匹配 * allMatch-检查是否匹配所有元素 * anyMatch-检查是否至少匹配一个元素 * noneMatch-检查是否没有匹配所有元素 * findFirst-返回第一个元素 * findAny-返回当前流中的任意元素 * count-返回流中元素的总个数 * max-返回流中最大值 * min-返回流中最小值 */ List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3)); // 注意,当集合元素为0时返回true boolean allMatch = asList.stream().allMatch(emp-> emp.getEmployId() >= 1); System.out.println(allMatch); boolean anyMatch = asList.stream().anyMatch(emp-> emp.getEmployId() > 2); System.out.println(anyMatch); boolean noneMatch = asList.stream().noneMatch(emp-> emp.getEmployId() > 4); System.out.println(noneMatch); Optional<Employee> findFirst = asList.stream().findFirst(); System.out.println(findFirst.get()); Optional<Employee> findAny = asList.parallelStream().findAny(); System.out.println(findAny.get()); long count = asList.stream().count(); System.out.println(count); Optional<Employee> max = asList.stream().max((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId())); System.out.println(max.get()); Optional<Employee> min = asList.stream().min((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId())); System.out.println(min.get()); --------------------------------------------------------------------------------------------------------------------- // reduce操作: reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以将流中元素反复结合起来,得到一个值 List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); Integer count2 = list.stream() .reduce(0, Integer::sum); System.out.println(count2); Optional<Integer> sum = list.stream() .reduce(Integer::sum); System.out.println(sum.get()); --------------------------------------------------------------------------------------------------------------------- // collect操作:Collect-将流转换为其他形式,接收一个Collection接口的实现,用于给Stream中元素做汇总的方法 List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3)); List<Integer> collect = asList.stream().map(e->e.getEmployId()).collect(Collectors.toList()); System.out.println(collect); ``` ***** 原文链接:https://blog.csdn.net/qq_29411737/article/details/80835658