Java8 flatMap demo 代码直接可以运行
2017-07-05 08:59
531 查看
参考自:http://www.cnblogs.com/shenlanzhizun/p/6027042.html
package com.by.interfaces; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Student { public Student(long id, String name, int age, int grade, String major, String school) { this.id = id; this.name = name; this.age = age; this.grade = grade; this.major = major; this.school = school; } public Student(String name) { this.name = name; } public Student() { } private List<String> attributes = Arrays.asList("111", "222", "333"); /** * 学号 */ private long id; private String name; private int age; /** * 年级 */ private int grade; /** * 专业 */ private String major; /** * 学校 */ private String school; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public List<String> getAttributes() { return attributes; } public void setAttributes(List<String> attributes) { this.attributes = attributes; } } public class Main { public static void main(String[] args) { List<Student> students = new ArrayList<Student>(); students.add(new Student(20160001, "孔明4", 20, 1, "土木工程", "武汉大学")); students.add(new Student(20160002, "伯约", 21, 2, "信息安全", "武汉大学")); students.add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学")); students.add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学")); students.add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学")); students.add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学")); students.add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学")); students.add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学")); students.add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学")); students.add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学")); //mapToLong的使用demo double value = students.stream().filter(student -> "计算机科学".equals(student.getMajor())).mapToLong(aaa -> aaa.getId()).sum(); String[] strs = {"java8", "is", "easy", "to", "use"}; List<String[]> distinctStrs = Arrays.stream(strs) .map(str -> str.split("")) // 映射成为Stream<String[]> .distinct() .collect(Collectors.toList()); distinctStrs.forEach(strings -> Arrays.stream(strings).forEach(s -> System.out.println(s))); System.out.println("==================================map方式"); List<String> distinctStrsByFlatMap = Arrays.stream(strs) .map(str -> str.split("")) // 映射成为Stream<String[]> .flatMap(Arrays::stream) // 扁平化为Stream<String> .distinct() .collect(Collectors.toList()); distinctStrsByFlatMap.forEach(s -> System.out.println(s)); System.out.println("==================================flatMap的方式"); List<List<Student>> studentss = new ArrayList<>(); studentss.add(students); studentss.add(students); System.out.println(studentss.size()); System.out.println("经过了flatmap处理后的stream查看下尺寸是多少" + studentss.stream().flatMap(students1 -> students1.stream()).count()); System.out.println(studentss.stream().flatMap(students1 -> students1.stream()).max((o1, o2) -> (o1.getName().length() - o2.getName().length())).get().getName()); System.out.println("经过了flatMap进行了彻底的流处理查看下有多少流" + studentss.stream().flatMap(students1 -> students1.stream()).flatMap(student -> student.getAttributes().stream()).count()); studentss.stream().flatMap(students1 -> students1.stream()).flatMap(student -> student.getAttributes().stream()).forEach(s -> System.out.println(s)); } }
相关文章推荐
- flex编译的代码可以拷贝到java后台直接访问flex页面
- 【文件上传】uploadify插件配合java的使用,可以图片,按照步骤可以复制粘贴直接运行,2017年
- 打包一个可以直接利用java -jar ***就可以运行的jar包步骤
- java代码没有main方法也可以运行!
- 在java代码里写Map<String, ?>和Map<String, Object>都是可以用的,他们两个有什么区别?
- 用java实现验证码的生成,以下代码是一个controller,可以直接使用!
- 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。
- autocad.net将代码保存到图纸中,打开图纸后可以直接运行?
- 百度地图 带AK可直接运行的baidumapsdk demo
- genexus 中可以直接使用java代码
- android studio环境下代码混淆(混淆完了直接打包就可以运行)
- 直接复制,保存成jsp。java可以直接运行linux命令。
- 如何导出Java项目成为一个可以直接双击运行的jar文件的问题
- 一段代码可以让Python直接在Linux/Mac/Unix上运行
- 预加载2+网页加载条 可以直接复制代码运行,如果图片地址丢失的话可以自己替换掉
- Ubuntu17.10 下配置caffe 仅CPU i386可以直接apt install caffe-cpu,但是怎么运行mnist代码我懵逼了
- Java直接运行JavaScript代码或js文件
- spring 配置统一管理,代码可以直接运行。包含所有类和配置文件。
- 如何把代码中的SQL语句改成可以直接在数据库中运行的SQL语句