API
网上优秀例子 玩转Java8Stream(二、函数式接口)
流相关,可结合collections处理容器类
官方解释:支持顺序和并行聚合操作的一系列元素

常用方法

1:allMatch /anyMatch

  1. List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
  2. boolean b = integers.stream().allMatch(it -> it > 2);
  3. b = integers.stream().anyMatch(it -> it > 2);

2:数组之间转换

数组转列表

  1. int[] a = {1,2,3,4,5};
  2. ArrayList<Integer> collect = Arrays.stream(a).boxed().collect(Collectors.toCollection(ArrayList::new));

int[] Integer[]互相转换

  1. int[] a = {1,2,3,4,5};
  2. Integer[] integers = Arrays.stream(a).boxed().toArray(Integer[]::new);
  3. int[] ints = Arrays.stream(integers).mapToInt(it -> it).toArray();

3:collect *重要

用于将流进行规约操作,

两种实现

  1. <R, A> R collect(Collector<? super T, A, R> collector);
  2. <R> R collect(Supplier<R> supplier,
  3. BiConsumer<R, ? super T> accumulator,
  4. BiConsumer<R, R> combiner);

第一种:配合Collector对流操作

常见用法:网上优秀例子

配合 Collectors.groupingBy

  1. String[] a = {"!","2","#","¥"};
  2. /*将数组转成列表*/
  3. List<String> collect = Arrays.stream(a).collect(Collectors.toList());
  4. /*链接字符串*/
  5. String collect1 = Arrays.stream(a).collect(Collectors.joining());
  6. /*等价于*/
  7. String join = String.join("", a);
  8. /*分组*/
  9. ArrayList<User> objects = new ArrayList<>();
  10. objects.add(new User("张三",18,"杭州","135","男"));
  11. objects.add(new User("张4",20,"北京","189","女"));
  12. objects.add(new User("张5",30,"上海","166","男"));
  13. objects.add(new User("张6",40,"广州","190","女"));
  14. objects.add(new User("张7",50,"深圳","178","男"));
  15. // 分组统计
  16. Map<String, List<User>> collect2 = objects.stream().collect(Collectors.groupingBy(User::getSex));
  17. System.out.println(collect2);
  18. // 分组计数
  19. Map<String, Long> collect3 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.counting()));
  20. System.out.println(collect3);
  21. //多级分组
  22. Map<String, Map<String, List<User>>> collect4 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.groupingBy(
  23. User::getTel
  24. )));
  25. System.out.println(collect4);
  26. // 条件分组
  27. Map<String, Map<String, List<User>>> collect5 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.groupingBy(it -> {
  28. if (it.getAge() > 30) {
  29. return "太老了";
  30. } else {
  31. return "小年轻";
  32. }
  33. }))
  34. );
  35. System.out.println(collect5);
  36. // 分组求和
  37. Map<String, Integer> collect6 = objects.stream().collect(Collectors.groupingBy(User::getSex, Collectors.summingInt(
  38. User::getAge
  39. )));
  40. System.out.println(collect6);
  41. }

配合 Collectors.toMap 将两个列表合成一个map

  1. List<Integer> integers = Arrays.asList(1, 2, 1, 4, 5);
  2. List<String> integers1 = Arrays.asList("1","2","3","4","5");
  3. Map<Integer, String> collect2 = integers.stream().collect(Collectors.toMap(
  4. it->it,
  5. it->integers1.get(integers.indexOf(it)),
  6. (o,n)->o+n));
  7. // 第二中方法
  8. HashMap<Integer, String> collect3 = IntStream.range(0, 5).collect(HashMap::new,
  9. (map, it) -> map.merge(integers.get(it), integers1.get(it),(o,n)->o+n),
  10. HashMap::putAll);
  11. System.out.println(collect3);

第二种:网上例子

参数解释
Supplier supplier: 目标容器 ,lamdam表达式
BiConsumer accumulator 目标容器操作,如何将数据加入容器
BiConsumer combiner) 将多个容器(supplier)合并到一起

  1. ArrayList<User> objects = new ArrayList<>();
  2. objects.add(new User("张三",18,"杭州","135","男"));
  3. objects.add(new User("张4",20,"北京","189","女"));
  4. objects.add(new User("张5",30,"上海","166","男"));
  5. objects.add(new User("张6",40,"广州","190","女"));
  6. objects.add(new User("张7",50,"深圳","178","男"));
  7. HashMap<Object, Object> collect2 = objects.stream().collect(
  8. HashMap::new,
  9. (map, it) -> map.put(it.getName(), it),
  10. Map::putAll);
  11. System.out.println(collect2);
  12. //等价于
  13. Map<String, User> collect3 = objects.stream().collect(Collectors.toMap(it -> it.getName(), it -> it));
  14. System.out.println(collect3);

4:flatMap

例子

将多维数组 扁平化处理,类似 numpy中 flatten 对数组降维操作

  1. String[][] c = {{"1","2","3"},{"4","5","6"}};
  2. List<String[]> strings = Arrays.asList(c);
  3. Stream<String[]> stream = strings.stream();
  4. Stream<String> stringStream = stream.flatMap(Arrays::stream);
  5. List<String> collect3 = stringStream.collect(Collectors.toList());
  6. System.out.println(collect3);
  7. Integer[][] d = {{1,2,3},{4,5,6}};
  8. List<Integer[]> ints = Arrays.asList(d);
  9. Stream<Integer[]> stream1 = ints.stream();
  10. Stream<Integer> integerStream = stream1.flatMap(Arrays::stream);
  11. List<Integer> collect4 = integerStream.collect(Collectors.toList());
  12. System.out.println(collect4);

5: map。reduce,filter

同其他语言,对流中元素进行处理

6:IntStream.range生成顺序数

同python 中 range函数

  1. IntStream range = IntStream.range(0, 10);
  2. range.forEach(System.out::println);