1. Basic API for first knowledge of reduce
@Test Publicvoid Testreduce () {stream<integer> Stream = Arrays.stream (New integer[]{1,2,3,4,5,6,7,8});The collection element is only and Integer result = Stream.reduce (0,Integer::sum); SYSTEM.OUT.PRINTLN (result); stream = Arrays.stream (New integer[]{1,2,3,4,5,6,7});Sum Stream.reduce((I, j), I + j).Ifpresent(System.out::p rintln);stream =Arrays.Stream(New integer[]{1,2,3,4,5,6,7}); To find the maximum valueStream.Reduce(Integer::max).Ifpresent(System.out::p rintln);stream =Arrays.Stream(New integer[]{1,2,3,4,5,6,7}); To find the minimum valueStream.Reduce(Integer::min).Ifpresent(System.out::p rintln);stream =Arrays.Stream(New integer[]{1,2,3,4,5,6,7}); Do the logicStream.Reduce((I, j), I > J. j:i).Ifpresent(System.out::p rintln);stream =Arrays.Stream(new integer[]{1, 2, 3, 4, 5, 6, 7});//Find the logic to fly int result2 = STREAM.
filter
(i-i% 2 = = 0). Reduce(1, (i, j)-I * j); Optional. of(RESULT2). Ifpresent(system.out::p rintln);}
2. Application Scenario Testing
The sum of all students ' achievements.
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-2609:*/publicClassreducetest {@Test Publicvoid Reducelist () {list<student> List = getstudents ();Use reduce to add all of the scores and optional<score> Totalscore = List.stream (). Map (Student::getscore). Reduce((x, y), X.add (y));System.Out.println(Totalscore.get (). GetPoint ()); } @TestPublicvoidReduceList2() {list<Student>List =Getstudents();StudentStudent =Getstudent(); UseReduce demandList,The sum of student's total scoresScoreScoresum =List.Stream() .Map(Student::getscore)//is equivalent to adding an initial value.Reduce(Student.getscore (), (x, y), X.add (y));System.Out.println(Scoresum.getpoint ()); }PrivateStudentGetstudent() {StudentStudent =NewStudent();Student.SetId(4);ScoreScore =NewScore();Score.SetPoint(100);Student.SetScore(score);ReturnStudent }Privatelist<Student>Getstudents() {list<Student>List =NewArraylist<>();For(int i =0; I <3; i++) {StudentStu =Newstudent (); score score = new score (); score. setpoint (80); score. setcoursename ( "中文版"); stu. setid (i); stu. setscore (score); list. add (STU);} return list;}
Package Com.jd.bean;//student public class Student {private Integer ID; //course score 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;Course ScorePublicClass Score {ScoresPrivate Integer point;Course NamePrivate 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;}}
Basic use of reduce in Java8