Java8 中reduce的基本使用

Posted qinhao517

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8 中reduce的基本使用相关的知识,希望对你有一定的参考价值。

1、初识 reduce 的基本 api

    @Test
    public void testReduce() {
        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8});

        //求集合元素只和
        Integer result = stream.reduce(0, Integer::sum);
        System.out.println(result);

        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});

        //求和
        stream.reduce((i, j) -> i + j).ifPresent(System.out::println);


        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        //求最大值
        stream.reduce(Integer::max).ifPresent(System.out::println);

        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        //求最小值
        stream.reduce(Integer::min).ifPresent(System.out::println);

        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        //做逻辑
        stream.reduce((i, j) -> i > j ? j : i).ifPresent(System.out::println);

        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});

        //求逻辑求乘机
        int result2 = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);

        Optional.of(result2).ifPresent(System.out::println);
    }

 

 

2、应用场景测试

求所有学生的成绩之和。

package com.jd;
import com.jd.bean.Score;
import com.jd.bean.Student;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author: wangyingjie1
 * @version: 1.0
 * @createdate: 2017-09-26 09:35
 */
public class ReduceTest {

    @Test
    public void reduceList() {

        List<Student> list = getStudents();

        //使用Reduce 将所有的所有的成绩进行加和
        Optional<Score> totalScore = list.stream()
                .map(Student::getScore)
                .reduce((x, y) -> x.add(y));

        System.out.println(totalScore.get().getPoint());
    }


    @Test
    public void reduceList2() {

        List<Student> list = getStudents();

        Student student = getStudent();

        //使用Reduce 求 list 、student 的总成绩之和
        Score scoreSum = list.stream()
                .map(Student::getScore)
                //相当于加了一个初始值
                .reduce(student.getScore(), (x, y) -> x.add(y));

        System.out.println(scoreSum.getPoint());
    }

    private Student getStudent() {
        Student student = new Student();
        student.setId(4);
        Score score = new Score();
        score.setPoint(100);
        student.setScore(score);
        return student;
    }


    private List<Student> getStudents() {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Student stu = new Student();

            Score score = new Score();
            score.setPoint(80);
            score.setCourseName("English");

            stu.setId(i);
            stu.setScore(score);

            list.add(stu);
        }
        return list;
    }

}

 

package com.jd.bean;
//学生
public class Student {

    private Integer id;

    //课程分数
    private Score score;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }
}

 

package com.jd.bean;
//课程分数
public class Score {

    //分数
    private Integer point;

    //课程名称
    private String courseName;

    public Integer getPoint() {
        return point;
    }

    public Score add(Score other) {

        this.point += other.getPoint();

        return this;
    }

    public void setPoint(Integer point) {
        this.point = point;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
}

以上是关于Java8 中reduce的基本使用的主要内容,如果未能解决你的问题,请参考以下文章

几个关于js数组方法reduce的经典片段

几个关于js数组方法reduce的经典片段

精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!(转载)

java8 如何在stream之后判断使用哪个方法

《Java8实战》 - 读书笔记 - Stream流的基本用法

java8中Collectors.groupingBy下用Collectors.reducing的疑惑