【Java 8】Stream通过reduce()方法合并流为一条数据示例
在本页中,我们将提供 Java 8 Stream reduce()示例。
Stream reduce()
对流的元素执行缩减。它使用恒等式和累加器函数进行归约。
在并行处理中,我们可以将合并器函数作为附加参数传递给该方法。
Stream reduce()
可用于获取存储在集合中的数字的总和。
Stream reduce()
还可以用给定的分隔符连接存储在集合中的字符串数据。
Stream reduce()
方法可以根据需要执行更多的还原任务
下面我们来看一些例子。
单参数方法
Stream.reduce()做累加操作
Stream.reduce()默认使用BinaryOperator作为累加器(Accumulator)。如果是数字,则起始值为0。如果是字符串,则起始值将为空字符串。
reduce(BinaryOperator accumulator)
方法将返回Optional实例。找到例子。
ReduceDemo1.java
package com.concretepage;
import java.util.Arrays;
public class ReduceDemo1 {
public static void main(String[] args) {
int[] array = {23,43,56,97,32};
Arrays.stream(array).reduce((x,y) -> x+y).ifPresent(s -> System.out.println(s));
Arrays.stream(array).reduce(Integer::sum).ifPresent(s -> System.out.println(s));
Arrays.stream(array).reduce(StatisticsUtility::addIntData).ifPresent(s -> System.out.println(s));
}
}
StatisticsUtility.java
package com.concretepage;
public class StatisticsUtility {
public static int addIntData(int num1, int num2) {
return num1 + num2;
}
}
输出
251
251
251
双参数方法
Stream.reduce()带初始值的累加操作
这里我们将使用一个恒等式和累加器。我们将传递标识作为起始值。
reduce(T identity, BinaryOperator<T> accumulator)
来看一组示例
ReduceDemo2.java
package com.concretepage;
import java.util.Arrays;
public class ReduceDemo2 {
public static void main(String[] args) {
int[] array = {23,43,56,97,32};
//Set start value. Result will be start value + sum of array.
int startValue = 100;
int sum = Arrays.stream(array).reduce(startValue, (x,y) -> x+y);
System.out.println(sum);
sum = Arrays.stream(array).reduce(startValue, Integer::sum);
System.out.println(sum);
sum = Arrays.stream(array).reduce(startValue, StatisticsUtility::addIntData);
System.out.println(sum);
}
}
输出
351
351
351
三参数方法
Stream.reduce()与标识(Identity),累加器(Accumulator), 组合器(Combiner)使用
这里我们将在reduce()方法中传递三个参数identity、accumulator和combiner。标识值必须是组合器函数的标识。这种三参数方法用于并行处理。合并器只与并行流一起工作,否则就没有要合并的内容。Accumulator先处理后,Combiner再执行。
reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
ReduceDemo3.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class ReduceDemo3 {
public static void main(String[] args) {
List<Integer> list2 = Arrays.asList(5, 6, 7);
int res = list2.parallelStream().reduce(1, (s1, s2) -> s1 * s2, (p, q) -> p * q);
System.out.println(res);
}
}
输出
210
更多例子
将列表和数组缩减为字符串
ReduceToString.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class ReduceToString {
public static void main(String[] args) {
//Reduce Array to String.
String[] array = {"Mohan", "Sohan", "Mahesh"};
Arrays.stream(array).reduce((x, y) -> x +"," + y)
.ifPresent(s -> System.out.println("Array to String: "+ s));
//Reduce List to String.
List<String> list = Arrays.asList("Mohan", "Sohan", "Mahesh");
list.stream().reduce((x, y) -> x +"," + y)
.ifPresent(s -> System.out.println("List to String: "+ s));
}
}
输出
Array to String: Mohan,Sohan,Mahesh
List to String: Mohan,Sohan,Mahesh
将列表和数组缩减求和
ReduceToSum.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class ReduceToSum {
public static void main(String[] args) {
//Reduce Array to sum.
int[] array = {30, 10, 20, 40};
int sum = Arrays.stream(array).reduce(0, (x, y) -> x + y);
System.out.println("Sum of Array: "+ sum);
//Reduce List to sum.
List<Integer> list = Arrays.asList(30, 10, 20, 40);
sum = list.stream().reduce(0, (x, y) -> x + y);
System.out.println("Sum of List: "+ sum);
}
}
输出
Sum of Array: 100
Sum of List: 100
【Java 8】Stream通过reduce()方法合并流为一条数据示例的更多相关文章
- java 数据类型:集合接口Collection之 Stream 的reduce方法
Stream 的reduce递归计算 import java.util.ArrayList; import java.util.Arrays; import java.util.List; impor ...
- Java 线程池 +生产者消费者+MySQL读取300 万条数据
1.1需求 数据库300 万条用户数据 ,遍历获取所有用户, 各种组合关联, 获取到一个新的json ,存到redis 上. 1.2 难点 数据库比较多, 不可能单线程查询所有的数据到内存. 1.3解 ...
- java 21 - 13 IO流之 合并流
SequenceInputStream :表示其他输入流的逻辑串联. 构造方法摘要 SequenceInputStream(Enumeration<? extends InputStream&g ...
- JAVA List根据字段排序以及取前几条数据
1.经常会遇到对组装的list排序或提取list中前几条数据,例如: 根据时间排序: list.sort((o1, o2) -> o2.getCreateTime().compareTo(o1. ...
- Java中如何实现j并发更新数据库同一条数据
分情况来说:普通单应用并发.多应用或多台服务器并发 情况一:普通单应用并发 使用关键字synchronized就可实现. 情况二:多应用或多台服务器并发 因多个应用之间并非同一个jvm(应用)内,因此 ...
- [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用
reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
- Java 8 新特性-菜鸟教程 (5) -Java 8 Stream
Java 8 Stream Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种 ...
- java.util.stream 库简介
Java Stream简介 Java SE 8 中主要的新语言特性是拉姆达表达式.可以将拉姆达表达式想作一种匿名方法:像方法一样,拉姆达表达式具有带类型的参数.主体和返回类型.但真正的亮点不是拉姆达表 ...
- Java 8 Stream流编程学习
本文是自己学习菜鸟教程中总结的笔记,用于快速找代码,完整的文档见菜鸟教程:Java 8 Stream Stream 使用一种类似用SQL语句从数据库查询数据的直观方式来提供一种对Java集合运算和表达 ...
随机推荐
- tomcat9启动报错too low setting for -Xss
在tomcat下部署war包启动时报错,关键错误信息如下: Caused by: java.lang.IllegalStateException: Unable to complete the sca ...
- Spring 之 BeanFactory 源码 - 接口分析
一.BeanFactory的基本类体系结构(接口为主):
- oracle合并列的函数wm_concat的使用详解
oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_concat(column)函数实现字段合并,如果您对oracle wm_concat( ...
- Part 38 AngularJS cancel route change
n this video we will discuss, how to cancel route change in Angular with an example. This is extreme ...
- 大爽Python入门教程 2-2 序列: 字符串、元组与列表
大爽Python入门公开课教案 点击查看教程总目录 序列 序列(sequence): 顾名思义,有序的排列. 有序排列的一串数据. 一种容器,容器内成员有序排列. python的字符串str,元组tu ...
- 在Winform中直接录入表格数据和在Vue&Elment中直接录入表格数据的比较
一般来说,录入数据的时候,我们都采用在一个窗体界面中,根据不同内容进行录入,但是有时候涉及主从表的数据录入,从表的数据有时候为了录入方便,也会通过表格控件直接录入.在Winform开发的时候,我们很多 ...
- Swift-技巧(五)设置圆角的代码
摘要 实现控件圆角的代码时,会不假思索的写 cornerRadius 和 masksToBounds,因为搜索得到的设置圆角的代码就是这样.今天突发奇想,为什么要写 masksToBounds? 打个 ...
- 【Rancher相关问题】Rancher 2.5.8 及以下版本,提示Alert: Component controller-manager,scheduler is unhealthy.
问题描述 如图,Rancher2.5.8版本提示 controller-manager,scheduler 不健康,管理的k8s集群版本1.21.1 解决方法 在Master节点执行如下命令: sed ...
- springbootjpa的dao层也会出现找不到javabean的操作
使用jpa的过程中,有一次使用dao写了一个 SysCompany findByName(String name);但实体类中没有name这个属性就会报错.bean注入异常
- NFLSOJ #10317. -「2020联考北附2」三千世界(找等价表达+树形 dp)
题面传送门 出题人可能原本感觉没啥难度的 T2 竟然变成了防 AK 题,奇迹奇迹( 首先带着这个 \(\max\) 肯定不太好处理,考虑找出 \(f(S)\) 的等价表达.我们考虑以 \(1\) 为根 ...