码迷,mamicode.com
首页 > 其他好文 > 详细

Stream系列(八)Reduce方法使用

时间:2019-12-01 20:39:44      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:final   ted   lan   package   win   utils   cte   bsp   视频   

裁减计算

视频讲解:  https://www.bilibili.com/video/av77715582/

技术图片

EmployeeTest.java
package com.example.demo;

import lombok.extern.log4j.Log4j2;
import org.junit.Test;

import javax.swing.plaf.basic.BasicTreeUI;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@Log4j2
public class EmployeeTest extends BaseTestCase {
    @Test
    public void reduce(){
        double dSum = list.stream().map(Employee::getSalary).reduce(Double::sum).get();
        assertTrue(dSum == 8000);
        double dSum2 = list.stream().map(Employee::getSalary).reduce((a,b) ->a + b).get();
        assertTrue(dSum == dSum2);
        double dSum3 = list.stream().map(Employee::getSalary).reduce((a,b) ->{
            System.out.println("a:"+a+" b"+b);
            return a + b;
        }).get();
        assertTrue(dSum == dSum3);
        double dSum4 = list.stream().map(Employee::getSalary).reduce(AddUtils::add).get();
        assertTrue(dSum == dSum4);
        double dSum5 = list.stream().map(Employee::getSalary).reduce(0.00,Double::sum);
        assertTrue(dSum == dSum5);
    }
    static class AddUtils {
        public static Double add(Double a, Double b) {
            return a + b;
        }
    }
}
BaseTestCase.java
package com.example.demo;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

关注公众号,坚持每天3分钟学习

技术图片

 

Stream系列(八)Reduce方法使用

标签:final   ted   lan   package   win   utils   cte   bsp   视频   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11967294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!