Java8 中reduce的基本使用
来源:互联网 发布:网络监控如何设置 编辑:程序博客网 时间:2024/06/08 07:11
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; }}
阅读全文
0 0
- Java8 中reduce的基本使用
- JAVA8-LAMBDA中reduce的用法
- Java8 stream的reduce说明
- java8中lambda的使用
- Java FP(Java8): Java中函数式编程的Map和Fold(Reduce)
- java8中Lambda表达式的使用
- java8中 关于Base64使用的处理
- Java8 中Optional类的使用
- java8中双冒号的使用
- Java8中Stream基本用法
- Java8中聚合操作collect、reduce方法详解
- Java8中聚合操作collect、reduce方法参数作用
- eclipse中使用java8
- Java8中使用APT
- eclipse中使用java8
- Swift 数组中 Map,FlatMap,Filter,Reduce的使用
- Python中filter、lambda、map、reduce的使用
- Swift 数组中 Map,FlatMap,Filter,Reduce的使用
- 在IDEA中创建Java 9 modules(模块)的Hello World
- 使用本地计划任务定时关闭azure虚拟机
- Swift Label计算高度
- 关于 Java 数组的 12 个最佳方法
- 排序与二分查找
- Java8 中reduce的基本使用
- Unity将麦克风获取的音频实时的处理成可视化的音波
- jdk+tomcat安装
- Python序列的切片操作与技巧
- [How to] 使用HBase协处理器---基本概念和regionObserver的简单实现
- 浏览器内核信息汇总和
- Deep Learning模型之:CNN卷积神经网络(二)文字识别系统LeNet-5
- Java中HashMap的实现原理
- perl 调用自己写的pm