Java8 Stream API总结

Stream

流的理解:4.2 - 4.3。
参考了他人的总结:Stream总结,中间操作,结束操作等分类以及介绍可以参阅该文。

构建流

1.1 使用Collection下的 stream() 和 parallelStream() 方法

List<String> list = new ArrayList<>();
Stream<String> stream = list.stream(); //获取一个顺序流
Stream<String> parallelStream = list.parallelStream(); //获取一个并行流

1.2 使用Arrays 中的 stream() 方法,将数组转成流

Integer[] nums = new Integer[10];
Stream<Integer> stream = Arrays.stream(nums);

1.3 使用Stream中的静态方法:of()、iterate()、generate()。后两个不指定limit的话可以创建所谓的无限流。
iterate是在上次产生的值的基础上引用指定的lambda表达式生成新的值。
generate不是以上次的值来产生新值,它会每次调用Supplier< T >类型的Lambda提供一个崭新的值。

Stream<Integer> stream = Stream.of(1,2,3,4,5,6);
 
Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
stream2.forEach(System.out::println); // 0 2 4 6 8 10
 
Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
stream3.forEach(System.out::println);

1.4 使用 BufferedReader.lines() 方法,将每行内容转成流

BufferedReader reader = new BufferedReader(new FileReader("F:\\test_stream.txt"));
Stream<String> lineStream = reader.lines();
lineStream.forEach(System.out::println);

1.5 使用 Pattern.splitAsStream() 方法,将字符串分隔成流

Pattern pattern = Pattern.compile(",");
Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
stringStream.forEach(System.out::println);

筛选与切片

filter:过滤流中的某些元素,接受一个谓词(一个返回boolean的函数)作为参数,并返回一个包括所有符合谓词的元素的流。
distinct:通过流中元素的 hashCode() 和 equals() 去除重复元素
limit(n):获取n个元素,是否是前N个取决于源本身是否是有序的
skip(n):跳过n元素,配合limit(n)可实现分页,如果不足n个,则返回空流

Stream<Integer> stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
 
Stream<Integer> newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14
        .distinct() //6 7 9 8 10 12 14
        .skip(2) //9 8 10 12 14
        .limit(2); //9 8
newStream.forEach(System.out::println);

映射

map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
flatMap存在的原因如下图:期望的是Stream< String >,得到的是Stream<String[]>
在这里插入图片描述
在这里插入图片描述

上述是“创建”一个新的版本出来,而不是去“修改”

List<String> list = Arrays.asList("a,b,c", "1,2,3");
 
//将每个元素转成一个新的且不带逗号的元素
Stream<String> s1 = list.stream().map(s -> s.replaceAll(",", ""));
s1.forEach(System.out::println); // abc  123
 
Stream<String> s3 = list.stream().flatMap(s -> {
    
    
    //将每个元素转换成一个stream
    String[] split = s.split(",");
    Stream<String> s2 = Arrays.stream(split);
    return s2;
});
s3.forEach(System.out::println); // a b c 1 2 3

查找、匹配与聚合

allMatch:接收一个 Predicate 函数,当流中每个元素都符合该断言时才返回true,否则返回false
noneMatch:接收一个 Predicate 函数,当流中每个元素都不符合该断言时才返回true,否则返回false
anyMatch:接收一个 Predicate 函数,只要流中有一个元素满足该断言则返回true,否则返回false
findFirst:返回流中第一个元素
findAny:返回流中的任意元素
count:返回流中元素的总个数
max:返回流中元素最大值
min:返回流中元素最小值

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
 
boolean allMatch = list.stream().allMatch(e -> e > 10); //false
boolean noneMatch = list.stream().noneMatch(e -> e > 10); //true
boolean anyMatch = list.stream().anyMatch(e -> e > 4);  //true
 
Integer findFirst = list.stream().findFirst().get(); //1
Integer findAny = list.stream().findAny().get(); //1
 
long count = list.stream().count(); //5
Integer max = list.stream().max(Integer::compareTo).get(); //5
Integer min = list.stream().min(Integer::compareTo).get(); //1

归约

Optional reduce(BinaryOperator accumulator):第一次执行时,accumulator函数的第一个参数为流中的第一个元素,第二个参数为流中元素的第二个元素;第二次执行时,第一个参数为第一次函数执行的结果,第二个参数为流中的第三个元素;依次类推,直到最后一个元素。
T reduce(T identity, BinaryOperator accumulator):流程跟上面一样,只是第一次执行时,accumulator函数的第一个参数为identity,而第二个参数为流中的第一个元素。
U reduce(U identity,BiFunction<U, ? super T, U> accumulator,BinaryOperator< U > combiner):在串行流(stream)中,该方法跟第二个方法一样,即第三个参数combiner不会起作用。在并行流(parallelStream)中,我们知道流被fork join出多个线程进行执行,此时每个线程的执行流程就跟第二个方法reduce(identity,accumulator)一样,而第三个参数combiner函数,则是将每个线程的执行结果当成一个新的流,然后使用第一个方法reduce(accumulator)流程进行规约。

// 求和
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
// 求积
int product = numbers.stream().reduce(1, (a, b) -> a * b);
// 求集合中的最大值
Optional<Integer> max = numbers.stream().reduce(Integer::max);

//经过测试,当元素个数小于24时,并行时线程数等于元素个数,当大于等于24时,并行时线程数为16
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
 
Integer v = list.stream().reduce((x1, x2) -> x1 + x2).get();
System.out.println(v);   // 300
 
Integer v1 = list.stream().reduce(10, (x1, x2) -> x1 + x2);
System.out.println(v1);  //310
 
Integer v2 = list.stream().reduce(0,
        (x1, x2) -> {
    
    
            System.out.println("stream accumulator: x1:" + x1 + "  x2:" + x2);
            return x1 - x2;
        },
        (x1, x2) -> {
    
    
            System.out.println("stream combiner: x1:" + x1 + "  x2:" + x2);
            return x1 * x2;
        });
System.out.println(v2); // -300
 
Integer v3 = list.parallelStream().reduce(0,
        (x1, x2) -> {
    
    
            System.out.println("parallelStream accumulator: x1:" + x1 + "  x2:" + x2);
            return x1 - x2;
        },
        (x1, x2) -> {
    
    
            System.out.println("parallelStream combiner: x1:" + x1 + "  x2:" + x2);
            return x1 * x2;
        });
System.out.println(v3); //197474048

收集:规约汇总/分组分区/转换

collect:接收一个Collector实例,将流中元素收集成另外一个数据结构。
Collector主要提供了三大功能:

  1. 将流元素归约和汇总为一个值

  2. 元素分组

  3. 元素分区
    还可以借此将集合转化为其他集合
    Collector<T, A, R> 是一个接口,有以下5个抽象方法:
    在这里插入图片描述

  4. Supplier< A > supplier():创建一个结果容器A

  5. BiConsumer<A, T> accumulator():消费型接口,第一个参数为容器A,第二个参数为流中元素T。

  6. BinaryOperator< A > combiner():函数接口,该参数的作用跟上一个方法(reduce)中的combiner参数一样,将并行流中各个子进程的运行结果(accumulator函数操作后的容器A)进行合并。

  7. Function<A, R> finisher():函数式接口,参数为:容器A,返回类型为:collect方法最终想要的结果R。

  8. Set characteristics():返回一个不可变的Set集合,用来表明该Collector的特征。有以下三个特征:
    CONCURRENT:表示此收集器支持并发。(官方文档还有其他描述,暂时没去探索,故不作过多翻译)
    UNORDERED:表示该收集操作不会保留流中元素原有的顺序。
    IDENTITY_FINISH:表示finisher参数只是标识而已,可忽略。

支持自定义收集器转换器,详细参见《Java8实战》6.5、6.6节。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
根据《Effective Java》46条建议(P168),尽量不要使用counting、summing、averaging、summarizing、reducing这些方法,他们复制了Stream已有的API、只是为了提供一种下游收集器。不建议使用。

Student s1 = new Student("aa", 10,1);
Student s2 = new Student("bb", 20,2);
Student s3 = new Student("cc", 10,3);
List<Student> list = Arrays.asList(s1, s2, s3);
 
 // 转换操作
//装成list
List<Integer> ageList = 
	list.stream().map(Student::getAge)
	.collect(Collectors.toList()); // [10, 20, 10]
 
//转成set
Set<Integer> ageSet = 
	list.stream().map(Student::getAge)
	.collect(Collectors.toSet()); // [20, 10]
 
//转成map,注:key不能相同,否则报错
Map<String, Integer> studentMap = 
	list.stream()
	.collect(Collectors.toMap(Student::getName, Student::getAge));
	 // {cc=10, bb=20, aa=10}
 
//字符串分隔符连接
String joinName = 
	list.stream().map(Student::getName)
	.collect(Collectors.joining(",", "(", ")")); // (aa,bb,cc)

 //。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
// 规约汇总操作
//1.学生总数
Long count = list.stream().collect(Collectors.counting()); // 3
// Long count = list.stream().count();
//2.最大年龄 (最小的minBy同理),接收一个Comparator参数(自定义)用以进行比较
Comparator<Dish> dishCaloriesComparator =
Comparator.comparingInt(Dish::getCalories);
Optional<Dish> mostCalorieDish = menu.stream()
	.collect(maxBy(dishCaloriesComparator));
// Comparator的默认实现
Integer maxAge = list.stream().map(Student::getAge)
.collect(Collectors.maxBy(Integer::compare)).get(); // 20
//3.所有人的年龄
Integer sumAge = list.stream()
.collect(Collectors.summingInt(Student::getAge)); // 40
//4.平均年龄
Double averageAge = list.stream()
.collect(Collectors.averagingDouble(Student::getAge)); // 13.333333333333334
// 包含上述所有方法
DoubleSummaryStatistics statistics = 
	list.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("count:" + statistics.getCount() 
+ ",max:" + statistics.getMax() + ",sum:" + statistics.getSum()
+ ",average:" + statistics.getAverage());
//规约
Integer allAge = list.stream().map(Student::getAge)
.collect(Collectors.reducing(Integer::sum)).get(); //40
int totalCalories = menu.stream().mapToInt(Dish::getCalories).sum();

//。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
//分组操作
Map<Integer, List<Student>> ageMap = 
	list.stream().collect(Collectors.groupingBy(Student::getAge));
//另一个分组例子:
public enum CaloricLevel {
    
     DIET, NORMAL, FAT }
Map<CaloricLevel, List<Dish>> dishesByCaloricLevel = menu.stream().collect(
	groupingBy(dish -> {
    
    
		if (dish.getCalories() <= 400) return CaloricLevel.DIET;
		else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
		else return CaloricLevel.FAT;
		}));
//多重分组,先根据类型分再根据年龄分,groupingBy第二个参数如果不选择默认返回的是list
Map<Integer, Map<Integer, List<Student>>> typeAgeMap = 
	list.stream()
	.collect(Collectors.groupingBy(Student::getType,Collectors.groupingBy(Student::getAge)));

Map<Dish.Type, Map<CaloricLevel, List<Dish>>> dishesByTypeCaloricLevel =
menu.stream().collect(
	groupingBy(Dish::getType,
	groupingBy(dish -> {
    
    
		if (dish.getCalories() <= 400) return CaloricLevel.DIET;
		else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
		else return CaloricLevel.FAT;} )));
// 第二个参数选择为某个类
Map<Dish.Type, Dish> mostCaloricByType =
	menu.stream()
		.collect(groupingBy(Dish::getType,
				collectingAndThen(
					maxBy(comparingInt(Dish::getCalories)),
					Optional::get)));
// 第二个参数选择为integer
Map<Dish.Type, Integer> totalCaloriesByType =
menu.stream().collect(groupingBy(Dish::getType,
summingInt(Dish::getCalories)));
// 第二个参数选择映射为set
Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType =
menu.stream().collect(
groupingBy(Dish::getType, mapping(
	dish -> {
    
     if (dish.getCalories() <= 400) return CaloricLevel.DIET;
		else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
		else return CaloricLevel.FAT; },
	toSet() )));

//分区操作
//分成两部分,一部分大于10岁,一部分小于等于10岁,partitioningBy也可以接收两个参数
// partitioningBy需要一个谓词,也就是返回一个布尔值的函数。
Map<Boolean, List<Student>> partMap = 
list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
Map<Boolean, List<Dish>> partitionedMenu =
menu.stream().collect(partitioningBy(Dish::isVegetarian));
Map<Boolean, Map<Dish.Type, List<Dish>>> vegetarianDishesByType =
menu.stream().collect(
partitioningBy(Dish::isVegetarian,
groupingBy(Dish::getType)));
Map<Boolean, Dish> mostCaloricPartitionedByVegetarian =
menu.stream().collect(
partitioningBy(Dish::isVegetarian,
collectingAndThen(
maxBy(comparingInt(Dish::getCalories)),
Optional::get)));

数值流

参见Java8实战5.6节
Java 8引入了三个原始类型特化流接口来解决这个问题: IntStream、 DoubleStream和LongStream,分别将流中的元素特化为int、 long和double,从而避免了暗含的装箱成本。

// 映射到数值流
OptionalInt maxCalories = menu.stream()
.mapToInt(Dish::getCalories)
.max();
int max = maxCalories.orElse(1);

// 转换为对象流,int -》 integer
IntStream intStream = menu.stream().mapToInt(Dish::getCalories);
Stream<Integer> stream = intStream.boxed();

并行流

并行流就是一个把内容分成多个数据块,并用不同的线程分别处理每个数据块的流。实际上背后借用了Fork/Join框架。默认的线 程 数 量 就 是 你 的 处 理 器 数 量 , 这 个 值 是由 Runtime.getRuntime().availableProcessors()得到的。
可 以 通 过 系 统 属 性 java.util.concurrent.ForkJoinPool.common.parallelism来改变线程池大小,这是一个全局设置,因此它将影响代码中所有的并行流。如下所示:

System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","12");

对顺序流调用parallel方法在内部实际上就是设了一个boolean标志,表示你想让调用parallel之后进行的所有操作都并行执行。类似地,你只需要对并行流调用sequential方法就可以把它变成顺序流。
以最后一次设置为准。

并行化并不是没有代价的。很重要的一点是要保证在内核中并行执行工作的时间比在内核之间传输数据的时间长。

高效使用并行流的几个注意事项:

  1. 留意装箱。自动装箱和拆箱操作会大大降低性能。 尽量用原始类型流 IntStream、LongStream、 DoubleStream。
  2. 有些操作本身在并行流上的性能就比顺序流差。特别是limit和findFirst等依赖于元素顺序的操作,它们在并行流上执行的代价非常大。可以调用unordered方法来把有序流变成无序流。
  3. 对于较小的数据量,选择并行流几乎从来都不是一个好的决定
  4. 要考虑流背后的数据结构是否易于分解。例如, ArrayList的拆分效率比LinkedList高得多。另外,用range工厂方法创建的原始类型流也可以快速分解。
  5. 非线程安全的方法不能用并行流执行。

《Effective Java》48条给出了使用并行流的一些非常有用的建议。

自定义Fork/Join的方法,参见《Java8实战》的7.3节。

猜你喜欢

转载自blog.csdn.net/weixin_38370441/article/details/112261221