Java8 中reduce的基本使用

來源:互聯網
上載者:User

標籤:gets   int()   package   情境   lte   str   ack   tle   基本使用   

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的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.