# stream流中reduce()求和()-其他

## stream流中reduce()求和()

### reduce() 有三个参数类型

• 一个参数：Optional reduce(BinaryOperator accumulator)，传入求和函数式，
• 两个参数：T reduce(T identity, BinaryOperator accumulator)，（默认值，求和函数式）
• 三个参数： 待补充

### 1、普通数字求和

`````` public static void test2(){
List<Integer> list= Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9});
Integer sum=list.stream().reduce((x,y)->x+y).get();
System.out.println(sum);
}``````

### 2、BigDecimal求和

`````` public static void main(String[] args) {

List<User> list=new ArrayList<>();

User user1=new User();
user1.setNum1(new BigDecimal(123));
user1.setNum2(new BigDecimal(100));

User user2=new User();
user2.setNum1(new BigDecimal(100));
user2.setNum2(new BigDecimal(100));
System.out.println(sum);
}``````

``````BigDecimal sum=list.stream().map(User::getNum1)  //返回num1的列表

### stream().reduce()多字段求和

``````public static void main(String[] args) {

List<User> list=new ArrayList<>();

User user1=new User();
user1.setNum1(new BigDecimal(123));
user1.setNum2(new BigDecimal(100));

User user2=new User();
user2.setNum1(new BigDecimal(100));
user2.setNum2(new BigDecimal(100));

User u=list.stream().reduce(new User(), (x,y)->{
User user=new User();
return user;
}).get();
System.out.println(u.getNum1()+"------------"+u.getNum2());
}``````

### 关于两个参数的说明

``T reduce(T identity, BinaryOperator<T> accumulator);``

``````List<Integer> intList = Arrays.asList(1,2,3);
Integer result2=intList.stream().reduce(100, Integer::sum);
log.info("{}",result2);``````

``````Integer result3=intList.parallelStream().reduce(100, Integer::sum);
log.info("{}",result3);``````

identity必须是accumulator函数的一个identity，也就是说必须满足：对于所有的t,都必须满足 accumulator.apply(identity, t) == t

identity必须是accumulator函数的一个identity，也就是说必须满足：对于所有的t,都必须满足 accumulator.apply(identity, t) == t

————————

### reduce() 有三个参数类型

• 一个参数：Optional reduce(BinaryOperator accumulator)，传入求和函数式，
• 两个参数：T reduce(T identity, BinaryOperator accumulator)，（默认值，求和函数式）
• 三个参数： 待补充

### 1、普通数字求和

`````` public static void test2(){
List<Integer> list= Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9});
Integer sum=list.stream().reduce((x,y)->x+y).get();
System.out.println(sum);
}``````

### 2、BigDecimal求和

`````` public static void main(String[] args) {

List<User> list=new ArrayList<>();

User user1=new User();
user1.setNum1(new BigDecimal(123));
user1.setNum2(new BigDecimal(100));

User user2=new User();
user2.setNum1(new BigDecimal(100));
user2.setNum2(new BigDecimal(100));
System.out.println(sum);
}``````

``````BigDecimal sum=list.stream().map(User::getNum1)  //返回num1的列表

### stream().reduce()多字段求和

``````public static void main(String[] args) {

List<User> list=new ArrayList<>();

User user1=new User();
user1.setNum1(new BigDecimal(123));
user1.setNum2(new BigDecimal(100));

User user2=new User();
user2.setNum1(new BigDecimal(100));
user2.setNum2(new BigDecimal(100));

User u=list.stream().reduce(new User(), (x,y)->{
User user=new User();
return user;
}).get();
System.out.println(u.getNum1()+"------------"+u.getNum2());
}``````

### 关于两个参数的说明

``T reduce(T identity, BinaryOperator<T> accumulator);``

``````List<Integer> intList = Arrays.asList(1,2,3);
Integer result2=intList.stream().reduce(100, Integer::sum);
log.info("{}",result2);``````

``````Integer result3=intList.parallelStream().reduce(100, Integer::sum);
log.info("{}",result3);``````

identity必须是accumulator函数的一个identity，也就是说必须满足：对于所有的t,都必须满足 accumulator.apply(identity, t) == t

identity必须是accumulator函数的一个identity，也就是说必须满足：对于所有的t,都必须满足 accumulator.apply(identity, t) == t