流的创建(演示常用的)
数组创建
Arrays.stream
Arrays的静态方法stream() 可以获取数组流
String[] arr = { "a", "b", "c", "d", "e", "f", "g" }; Stream<String> stream = Stream.of(arr); Stream<String> stream1 = Arrays.stream(arr);
集合创建
Collection接口提供了两个默认方法创建流:stream()、parallelStream()
List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); Stream<String> stream = list.stream();
值创建
Stream.of
使用静态方法 Stream.of(), 通过显示值创建一个流。它可以接收任意数量的参数。
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
函数创建
Stream.iterate
Stream.generate
使用静态方法 Stream.iterate() 和 Stream.generate()创建无限流。
Stream<Double> stream = Stream.generate(Math::random).limit(5);
Stream<Integer> stream1 = Stream.iterate(0, i -> i + 1).limit(5);
注意:使用无限流一定要配合limit截断,不然会无限制创建下去。
GroupBy和求最值示例
实体类:
package com.demo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; /** lombok的使用需要的依赖: <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency> */ @Data @AllArgsConstructor @NoArgsConstructor @ToString public class Student { private long id; private String name; private int age; private int grade; private String major; private String school; }
示例代码:
package com.demo; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; public class FileTest { public static void main(String[] args) { /*------------------复杂对象演示------------------------*/ List<Student> students = new ArrayList<Student>() { { add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学")); add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学")); add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学")); add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学")); add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学")); } }; // group by 统计用法示例 Map<String, Long> group2 = students.stream().collect( Collectors.groupingBy( Student::getSchool, Collectors.counting() ) ); //输出:{南京大学=1, 武汉大学=2, 华中科技大学=2} System.out.println(group2); TreeMap<String, Set<String>> collect = students.stream().collect( Collectors.groupingBy( Student::getSchool, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toSet()) ) ); //输出:{华中科技大学=[翼德, 奉孝], 南京大学=[丁奉], 武汉大学=[孔明, 玄德]} System.out.println(collect); Map<String, List<Student>> collect3 = students.stream().collect( Collectors.groupingBy( Student::getSchool ) ); /* 输出:{ 南京大学=[Student(id=20163001, name=丁奉, age=24, grade=5, major=土木工程, school=南京大学)], 武汉大学=[ Student(id=20160001, name=孔明, age=20, grade=1, major=土木工程, school=武汉大学), Student(id=20160003, name=玄德, age=22, grade=3, major=经济管理, school=武汉大学) ], 华中科技大学=[ Student(id=20161001, name=翼德, age=21, grade=2, major=机械与自动化, school=华中科技大学), Student(id=20161003, name=奉孝, age=23, grade=4, major=计算机科学, school=华中科技大学) ] } * */ System.out.println(collect3); // 求最大 最小值 Student studentMax = students.stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get(); Student studentMin = students.stream().min((s1, s2) -> s1.getAge() - s2.getAge()).get(); // 输出:Student(id=20163001, name=丁奉, age=24, grade=5, major=土木工程, school=南京大学) System.out.println(studentMax); // 输出:Student(id=20160001, name=孔明, age=20, grade=1, major=土木工程, school=武汉大学) System.out.println(studentMin); /*------------------基础数据类型演示------------------------*/ List<Integer> list = new ArrayList<Integer>() { { add(20); add(18); add(18); add(17); } }; Map<Integer, Long> collect1 = list.stream().collect( Collectors.groupingBy( (integer) -> integer, Collectors.counting() ) ); // 输出:{17=1, 18=2, 20=1} System.out.println(collect1); TreeMap<Integer, List<Integer>> collect2 = list.stream().collect( Collectors.groupingBy( Integer::intValue, TreeMap::new, Collectors.mapping((integer) -> integer, Collectors.toList()) ) ); // 输出:{17=[17], 18=[18, 18], 20=[20]} System.out.println(collect2); Integer max = list.stream().max(Integer::compareTo).get(); Integer min = list.stream().min(Integer::compareTo).get(); System.out.println(max);// 输出:20 System.out.println(min);// 输出:17 /*------------------map集合演示------------------------*/ List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>() { { add(new HashMap<String,Object>(){{ put("prdTyp","lon"); put("typ","2.3"); }}); add(new HashMap<String,Object>(){{ put("prdTyp","lon"); put("typ","1.0"); }}); add(new HashMap<String,Object>(){{ put("prdTyp","lpr"); put("typ","88"); }}); add(new HashMap<String,Object>(){{ put("prdTyp","lpr"); put("typ","99"); }}); } }; Map<Object, List<Map<String, Object>>> prdTyp = list2.stream().collect( Collectors.groupingBy( (map) -> map.get("prdTyp") ) ); /* 输出:{ lon=[{prdTyp=lon, typ=2.3}, {prdTyp=lon, typ=1.0}], lpr=[{prdTyp=lpr, typ=88}, {prdTyp=lpr, typ=99}] } */ System.out.println(prdTyp); TreeMap<Object, List<Object>> collect4 = list2.stream().collect( Collectors.groupingBy( (map) -> map.get("prdTyp"), TreeMap::new, Collectors.mapping((map) -> map.get("prdTyp"), Collectors.toList()) ) ); // 输出:{lon=[lon, lon], lpr=[lpr, lpr]} System.out.println(collect4); Optional<Map<String, Object>> mapMax = list2.stream().max( Comparator.comparing( (map) -> new BigDecimal((String) map.get("typ")) ) ); // mapMax直接输出:Optional[{prdTyp=lpr, typ=99}] System.out.println(mapMax.get().get("typ"));//99 Optional<Map<String, Object>> mapMin = list2.stream().min( (map1,map2)->new BigDecimal((String) map1.get("typ")).compareTo(new BigDecimal((String) map2.get("typ"))) ); // mapMin直接输出:Optional[{prdTyp=lon, typ=1.0}] System.out.println(mapMin.get().get("typ"));//1.0 } }