【Java SE】流
================================================================
1、创建流
2、过滤 filter
3、映射 map flatMap
4、分组 groupingBy
================================================================
import java.util.*; import java.util.stream.Collectors;
人对象
class Person { private String name; private int age; }
// 数组创建流 public static<T> Stream<T> of(T... values); // 空流 public static<T> Stream<T> empty();
基本流
short a = 1; short b = 2; Stream<Short> shortStream = Stream.of(a, b); Stream<Character> charStream = Stream.of('a', 'b'); byte c = 1; byte d = 2; Stream<Byte> byteStream = Stream.of(c, d); Stream<Boolean> booleanStream = Stream.of(true, false); Stream<Integer> integerStream = Stream.of(1,2,3); IntStream intStream = IntStream.of(1,2,3); int[] intArray1 = new int[] {1,2,3}; int[] intArray2 = new int[] {4,5,6}; IntStream intStream1 = Arrays.stream(intArray1); IntStream intStream2 = Arrays.stream(intArray2, 1, 2); IntStream stepIntStream = IntStream.range(0, 100);// 生成0 ~ 99 不含100 IntStream stepIntStream2 = IntStream.rangeClosed(0, 100);// 生成0 ~ 99 不含100 LongStream longStream = LongStream.of(1,2,3); LongStream longStepStream = LongStream.range(0, 100);// 生成0 ~ 99 不含100 LongStream longStepStream2 = LongStream.rangeClosed(0, 100);// 生成0 ~ 99 不含100 DoubleStream doubleStream = DoubleStream.of(1,2,3); java.util.Random random = new Random(); IntStream randomIntStream = random.ints(); LongStream randomLongStream = random.longs(); DoubleStream randomDoubleStream = random.doubles();
List<Person> list = new ArrayList<>(); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 11));// 滤过年龄大于15的人 List<Person> resultList = list.stream().filter(p -> p.getAge() > 15).collect(Collectors.toList());
List<Person> list = new ArrayList<>(); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 21)); // 映射出姓名 List<String> resultList = list.stream().map(Person::getName).collect(Collectors.toList()); System.out.println(resultList);
List<String> words = new ArrayList<>(); words.add("h,e,l,l,o"); words.add("w,o,r,l,d"); // map 的结果是流 List<Stream> resultStream = words.stream().map(w -> Stream.of(w.split(","))).collect(Collectors.toList()); // flatMap 的结果是字符 List<String> resultList = words.stream().flatMap(w -> Stream.of(w.split(","))).collect(Collectors.toList()); System.out.println(resultList);
List<Person> list = new ArrayList<>(); list.add(new Person("Alice", 10)); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 11)); list.add(new Person("Tom", 21));
名称分组
Map<String, List<Person>> groupName = list.stream().collect(Collectors.groupingBy(Person::getName)); System.out.println(groupName);
// 结果
{Tom=[Person{name='Tom', age='11'}, Person{name='Tom', age='21'}], Alice=[Person{name='Alice', age='10'}, Person{name='Alice', age='20'}]}
分组件数
Map<String, Long> groupCount = list.stream().collect(Collectors.groupingBy(Person::getName, Collectors.counting())); System.out.println(groupCount); // 结果 {Tom=2, Alice=2}
分组合计
Map<String, Integer> groupSum = list.stream().collect(Collectors.groupingBy(Person::getName, Collectors.summingInt(Person::getAge))); System.out.println(groupSum); // 结果 {Tom=32, Alice=30}
分组最大值
List<Person> list = new ArrayList<>(); list.add(new Person("Alice", 10)); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 11)); list.add(new Person("Tom", 21)); Map<String, Optional<Person>> groupMax = list.stream().collect(Collectors.groupingBy(Person::getName, Collectors.maxBy(Comparator.comparing(Person::getAge)))); Optional<Person> tom = groupMax.get("Tom"); tom.ifPresent(v -> System.out.println(v)); // 结果 Person{name='Tom', age='21'}
分组最小值
List<Person> list = new ArrayList<>(); list.add(new Person("Alice", 10)); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 11)); list.add(new Person("Tom", 21)); Map<String, Optional<Person>> groupMin = list.stream().collect(Collectors.groupingBy(Person::getName, Collectors.minBy(Comparator.comparing(Person::getAge)))); Optional<Person> tom = groupMin.get("Alice"); tom.ifPresent(v -> System.out.println(v)); // 结果 Person{name='Alice', age='10'}
分组映射比较
年龄分组后,映射出名字最长的人
List<Person> list = new ArrayList<>(); list.add(new Person("Jean", 10)); list.add(new Person("Alice", 20)); list.add(new Person("Tom", 20)); list.add(new Person("Bob", 30)); Map<Integer, Optional<String>> groupByAge = list.stream().collect(Collectors.groupingBy(Person::getAge, Collectors.mapping(Person::getName, Collectors.maxBy(Comparator.comparing(String::length))))); for (Map.Entry<Integer, Optional<String>> item : groupByAge.entrySet()) { Integer age = item.getKey(); Optional<String> optional = item.getValue(); optional.ifPresent(v -> System.out.println(v + " : " + age)); }
reduce 方法是一种用于从流中计算某个值的通用方法
List<Integer> scores = new ArrayList<>(); scores.add(10); scores.add(20); scores.add(30); Optional<Integer> sumOptional = scores.stream().reduce((x,y) -> x + y); int sum2 = scores.stream().reduce(0, (x,y) -> x + y);// 第一个参数是幺元值,0是加法的幺元值,没有幺元值就存在没有值的情况返回optional sumOptional.ifPresent(v -> System.out.println(v)); System.out.println(sum2);