Stream
Stream::max
(int
和 Integer
的不同)
1 2 3 4 5
| int intArr[] = new int[]{1, 2, 3}; Arrays.stream(intArr).max().getAsInt(); Integer integerArr[] = new Integer[]{1, 2, 3}; Arrays.stream(integerArr).max(Integer::compareTo).get();
|
Stream中 int
和 Integer
互相转换
mapToObj
: int
-> Integer
三种方式:mapToObj(Integer::valueOf)
, mapToObj(i->Integer.valueOf(i))
, mapToObj(i->i)
1 2 3
| int nums = new int[]{1, 2, 3}; List<Integer> list = Arrays.stream(nums).mapToObj(Integer::valueOf).collect(Collectors.toList());
|
mapToInt
: Integer
-> int
三种方式:mapToInt(Integer::intValue)
, mapToInt(i->(int)i)
, mapToInt(i->i)
1 2 3 4 5
| int[] nums = l.stream().mapToInt(i -> i).toArray();
list.stream().mapToInt(Integer::intValue).sum(); list.stream().mapToInt(i->i).sum();
|
创建Stream
1 2 3
| Integer[] arr = {1, 3, 2, 5, 4}; Stream<Integer> arrStream = Arrays.stream(arr);
|
1 2
| List<Integer> list = Arrays.asList(arr); Stream<Integer> listStream = list.stream();
|
生成器Stream::generate
和迭代器Stream::iterate
1 2 3 4 5 6
| Stream<Double> generate = Stream.generate(Math::random); generate.limit(5).forEach((a)-> System.out.println("a = " + a));
Stream<Integer> iterate = Stream.iterate(0, (a) -> a + 1); iterate.limit(5).forEach((a)-> System.out.println("a = " + a));
|
过滤器filter(func)
1 2 3
| list.stream().filter((a) -> a < 5).forEach(System.out::print); System.out.println();
|
映射map
1 2 3
| Stream<Stream<Integer>> mapResult = list.stream().map((a) -> Stream.of(new Integer[]{a, a})); mapResult.forEach((a)->{ a.forEach(System.out::print); }); System.out.println();
|
1 2 3 4
| Stream<Integer> flatMapResult = list.stream().flatMap((a) -> Stream.of(new Integer[]{a, a})); flatMapResult.forEach(System.out::print); System.out.println();
|
去重distinct()
对原集合排序(产生一个新集合)sort(func || Comparator):
子流和组合流
limit(n)
:提取前n个
skip(n)
:去掉前n个
concat(Stream, Stream)
:拼接两个Stream
peek(func)
:提取Stream当前元素,进行其它操作(例如打印)
聚合(聚合是终止操作,之后不可以进行其它流的操作)(forEach也是终止操作)
count
:获取流的大小
- (以下函数返回Optional类型,因为可能为null)
max()
:获取流的最大值
min()
:获取流的最小值
findFirst(func->boolean)
:找到第一个符合条件的值
findAny(func->boolean)
:将流分成很多组,并行找到第一个符合条件的值
- 自定义聚合
reduce
:
reduce(identity, accumulator, combiner)
1 2 3 4 5 6 7 8 9
| List<Integer> l = Arrays.asList(1,2,3); Integer reduce = l.stream().reduce(0, (r, a) -> r + a); System.out.println("reduce = " + reduce);
Integer reduce2 = l.stream().reduce(0, (r, a) -> r + a, (r1,r2)->r1+r2); System.out.println("reduce2 = " + reduce2);
Integer reduce3 = l.stream().reduce((r, a) -> r + a).get(); System.out.println("reduce3 = " + reduce3);
|
Optional类型
isPresent()
:是否存在
isPresent(func)
:如果存在,进行func操作
get()
:获取值
orElse(x)
:如果存在,获取值;如果不存在,获取默认值x
1 2 3 4 5 6
| Optional<Integer> max = list.stream().max(Integer::compare); max.ifPresent(System.out::println); System.out.println("max.isPresent() = " + max.isPresent()); System.out.println("max.get() = " + max.get()); Optional<Integer> empty = Optional.empty(); System.out.println("empty.orElse(-1) = " + empty.orElse(-1));
|
收集collect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| List<Integer> collect = list.stream().collect(Collectors.toList()); List<Integer> collect1 = list.stream().collect(ArrayList::new, ArrayList::add, ArrayList::addAll); List<Integer> collect2 = list.stream().collect(Collectors.toCollection(ArrayList::new));
Map<Integer, Integer> collect5 = list.stream().collect(Collectors.toMap((a) -> a.hashCode(), (a) -> a.intValue())); System.out.println("collect5 = " + collect5);
Map<String, String> collect6 = Stream.of(Locale.getAvailableLocales()).collect(Collectors.toMap( (l) -> l.getDisplayLanguage(), (l) -> l.getDisplayLanguage(l), (oldValue, newValue) -> oldValue )); System.out.println("collect6 = " + collect6);
Map<String, Set<String>> collect7 = Stream.of(Locale.getAvailableLocales()).collect(Collectors.toMap( (l) -> l.getDisplayCountry(), (l) -> Collections.singleton(l.getDisplayLanguage()), (oldValue, newValue) -> { Set<String> set = new HashSet(oldValue); set.addAll(newValue); return set; } )); System.out.println("collect7 = " + collect7);
|
- Collector的Join操作(类似python的join)
1 2
| String collect3 = list.stream().map(String::valueOf).collect(Collectors.joining(", ")); System.out.println("collect3 = " + collect3);
|
1 2 3 4
| IntSummaryStatistics collect4 = list.stream().collect(Collectors.summarizingInt(Integer::valueOf)); System.out.println("collect4 = " + collect4); System.out.println("collect4.getSum() = " + collect4.getSum()); System.out.println("collect4.getAverage() = " + collect4.getAverage());
|
分组和分片
1 2 3 4 5 6
| Map<String, List<Locale>> collect8 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getCountry)); System.out.println("collect8 = " + collect8);
System.out.println("collect8.get(\"CH\") = " + collect8.get("CH"));
|
- 当分组是两类(Boolean)时,使用PartitionBy会更快
1 2 3 4
| Map<Boolean, List<Locale>> collect9 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.partitioningBy(l -> l.getLanguage().equals("en"))); System.out.println("collect9 = " + collect9);
|
DownStream收集器,对分组结果产生的map的value类型进行处理
- GroupingBy和PartitioningBy产生的结果的默认value值类型时List,进行downstream处理,自定义value类型
1 2 3 4 5 6 7 8
| Map<String, Set<Locale>> collect10 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getCountry, Collectors.toSet())); System.out.println("collect10 = " + collect10);
Map<String, Long> collect11 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getCountry, Collectors.counting())); System.out.println("collect11 = " + collect11);
|
- groupingBy+mapping可以实现collect(Collectors.toMap())的作用
1 2 3 4 5
| Map<String, Set<String>> collect13 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getDisplayCountry, Collectors.mapping(Locale::getDisplayLanguage, Collectors.toSet()))); System.out.println("collect13 = " + collect13);
|
- groupingBy+mapping+自定义value类型
1 2 3 4 5 6 7
| Map<String, Optional<String>> collect12 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getCountry, Collectors.mapping(Locale::getLanguage, Collectors.maxBy(Comparator.comparing(String::length))) )); System.out.println("collect12 = " + collect12);
|
- groupingBy+reducing(分组聚合)
1 2 3 4 5 6 7 8 9 10 11
| Map<String, String> collect14 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getDisplayCountry, Collectors.reducing("", Locale::getDisplayLanguage, (result, a) -> result = result.length() == 0 ? a : result + "+" + a))); System.out.println("collect14 = " + collect14);
Map<String, String> collect15 = Stream.of(Locale.getAvailableLocales()).collect( Collectors.groupingBy(Locale::getDisplayCountry, Collectors.mapping(Locale::getDisplayLanguage, Collectors.joining("+")))); System.out.println("collect15 = " + collect15);
|
对一些原生类型提供专门接口
- IntStream(short、char、byte、boolean、int)、LongStream(long)、DoubleStream(float、double)
1 2 3 4 5 6 7 8 9 10 11
| int[] arr = {1, 3, 2, 5, 4}; IntStream stream = Arrays.stream(arr); IntStream intStream = IntStream.range(0, 10); intStream.forEach(System.out::print); Stream<Integer> integerStream = IntStream.range(0, 10).boxed(); ArrayList<Integer> collect = integerStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll); System.out.println("collect = " + collect); IntSummaryStatistics summary = IntStream.range(0, 10).boxed().collect( Collectors.summarizingInt(Integer::valueOf)); System.out.println("summary = " + summary);
|
并行
1 2 3 4 5 6 7 8
| IntStream.range(0, 10).boxed().parallel().limit(5).forEach(System.out::print); System.out.println(); IntStream.range(0, 10).boxed().unordered().limit(5).forEach(System.out::print); System.out.println(); IntStream.range(0, 10).boxed().parallel().unordered().limit(5).forEach(System.out::print); System.out.println(); IntStream.range(0, 10).boxed().unordered().parallel().limit(5).forEach(System.out::print); System.out.println();
|