当前位置:网站首页>Lambda终结操作reduce归并
Lambda终结操作reduce归并
2022-06-13 03:35:00 【Leon_Jinhai_Sun】
对流中的数据按照你指定的计算方式计算出一个结果。(缩减操作)
reduce的作用是把stream中的元素给组合起来,我们可以传入一个初始值,它会按照我们的计算方式依次拿流中的元素和初始化值进行计算,计算结果再和后面的元素计算。
reduce两个参数的重载形式内部的计算方式如下:
T result = identity;
for (T element : this stream)
result = accumulator.apply(result, element)
return result;
其中identity就是我们可以通过方法参数传入的初始值,accumulator的apply具体进行什么计算也是我们通过方法参数来确定的。
例子:
使用reduce求所有作者年龄的和
// 使用reduce求所有作者年龄的和
List<Author> authors = getAuthors();
Integer sum = authors.stream()
.distinct()
.map(author -> author.getAge())
.reduce(0, (result, element) -> result + element);
System.out.println(sum);
使用reduce求所有作者中年龄的最大值
// 使用reduce求所有作者中年龄的最大值
List<Author> authors = getAuthors();
Integer max = authors.stream()
.map(author -> author.getAge())
.reduce(Integer.MIN_VALUE, (result, element) -> result < element ? element : result);
System.out.println(max);
使用reduce求所有作者中年龄的最小值
// 使用reduce求所有作者中年龄的最小值
List<Author> authors = getAuthors();
Integer min = authors.stream()
.map(author -> author.getAge())
.reduce(Integer.MAX_VALUE, (result, element) -> result > element ? element : result);
System.out.println(min);
reduce一个参数的重载形式内部的计算
boolean foundAny = false;
T result = null;
for (T element : this stream) {
if (!foundAny) {
foundAny = true;
result = element;
}
else
result = accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty();
如果用一个参数的重载方法去求最小值代码如下:
// 使用reduce求所有作者中年龄的最小值
List<Author> authors = getAuthors();
Optional<Integer> minOptional = authors.stream()
.map(author -> author.getAge())
.reduce((result, element) -> result > element ? element : result);
minOptional.ifPresent(age-> System.out.println(age));
边栏推荐
- 300W pieces of MySQL data were written in the test, and they were broken between 1.6 and 2W each time. Then the following problems occurred:
- Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
- Union, intersection and difference sets of different MySQL databases
- C语言程序设计——从键盘任意输入一个字符串(可以包含:字母、数字、标点符号,以及空格字符),计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程,但能实现strlen()的功能。
- LVS四层负载均衡集群(5)LVS概述
- User and permission configuration in SQL Server database to ensure database security
- Peking University HP financial index - matching enterprise green innovation index 2011-2020: enterprise name, year, industry classification and other multi indicator data
- Spark core concepts: Master, worker, driver program, executor, RDDS
- Doris data import broker load
- [test development] automated test selenium (III) -- unittest framework analysis
猜你喜欢
Azure SQL db/dw series (12) -- using query store (1) -- report Introduction (1)
Tencent cloud instant messaging IM
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Jumpserver: user - system privileged user - Asset - authorization
基于华为云物联网设计的浇花神器(STM32+ESP8266)
Spark core concepts: Master, worker, driver program, executor, RDDS
Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
[test development] automated test selenium (III) -- unittest framework analysis
[azure data platform] ETL tool (8) - ADF dataset and link service
Understand the difference between reducebykey and groupbykey in spark
随机推荐
swap()
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Tencent cloud instant messaging IM
IP address and classification
Spark optimization - Troubleshooting
Oracle database management
300W pieces of MySQL data were written in the test, and they were broken between 1.6 and 2W each time. Then the following problems occurred:
【 développement d'essais 】 sélénium d'essais automatisés (Ⅲ) - - analyse du cadre unitest
Masa auth - overall design from the user's perspective
On interests and hobbies
To resolve project conflicts, first-class project managers do so
A data modeling optimization solution for graph data super nodes
年金险产品保险期满之后能领多少钱?
C语言程序设计——从键盘任意输入一个字符串(可以包含:字母、数字、标点符号,以及空格字符),计算其实际字符个数并打印输出,即不使用字符串处理函数strlen()编程,但能实现strlen()的功能。
Yolov5 face+tensorrt: deployment based on win10+tensorrt8.2+vs2019
Installing MySQL 8.0.20 under Linux and ubuntu20.04 LTS
【测试开发】博客系统——Loadrunner性能测试(发布博客功能 基准测试)
Oracle built-in functions
The most complete ongdb and neo4j resource portal in history
Byte stream & buffer stream