Java数据采集新选择:DCS数据采集工具

The new class java.util.stream.Collectors in Java 8 implements the java.util.stream.Collector interface, and provides a large number of methods to perform map and reduce operations, or statistical operations on stream elements. In this chapter, we will take a look at some commonly used methods and write a few examples to practice.


Collectors.averagingDouble(): This method calculates the average of all elements in the stream when viewed as double type elements. The method returns the same Collectors instance, allowing for chained operations. It takes a parameter, which is a lambda expression used to map all elements before executing the averaging operation.


For example, the code below uses the collect() method to collect all elements and then passes them to the Collectors.averagingDouble(d->d*2) collector, which performs a *2 operation on each element before calculating the average value.


Collectors.averagingInt() and Collectors.averagingLong() work similarly to Collectors.averagingDouble(), but they treat the elements as int and long types respectively.


Collectors.collectingAndThen() is similar to map and reduce. It takes two parameters, the first one for reduce operation, and the second one for map operation. It first passes all elements in the stream to the second parameter, generates a collection, and then passes it to the first parameter for processing.


Collectors.counting() is used to count the number of elements in the stream.


Collectors.joining() combines all elements into a single string, with optional prefix and suffix.


Collectors.maxBy() and Collectors.minBy() are used to calculate the maximum and minimum values in the stream, and can accept a comparator as an argument for how to calculate these values.


Collectors.summingInt(), Collectors.summingLong(), and Collectors.summingDouble() calculate the sum of all elements in the stream, considering them as int, long, and double types respectively.


Collectors.toList(), Collectors.toSet(), and Collectors.toMap() are used to collect elements in the stream into a list, set, and map, respectively.


Collectors.mapping() is generally used in multiple map and reduce operations. It takes a function for mapping and a downstream collector for reduction.


Finally, we have a sample code that demonstrates the use of Collectors in Java 8 for various operations.

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空