java8新特性 --- stream(二)
来源:互联网 发布:在淘宝开店流程 编辑:程序博客网 时间:2024/05/21 21:16
2、映射
2.1 map 利用此功能可以输出我们需要的东西
将学生实体映射成学生姓名
List<String> names = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .map(Student::getName).collect(Collectors.toList());除了上面这类基础的map,java8还提供了mapToDouble(ToDoubleFunction<? super T> mapper),mapToInt(ToIntFunction<? super T> mapper),mapToLong(ToLongFunction<? super T> mapper)
计算‘计算机科学’专业的学生年龄之和
int totalAge = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .mapToInt(Student::getAge).sum();使用这些数值流的好处还在于可以避免jvm装箱操作所带来的性能消耗。
2.2 flatMap 用于合并用
/** * 测试flatMap类似于addAll的功能 */ @Test public void flatMapTest() { List<Integer> collected0 = new ArrayList<>(); collected0.add(1); collected0.add(3); collected0.add(5); List<Integer> collected1 = new ArrayList<>(); collected1.add(2); collected1.add(4); collected1 = Stream.of(collected0, collected1) .flatMap(num -> num.stream()).collect(Collectors.toList()); System.out.println(collected1);// 1,3,5,2,4 }3、查找
3.1 allMatch 用于检测是否全部都满足指定的参数行为,如果全部满足则返回true
检测是否所有的学生都已满18周岁
boolean isAdult = students.stream().allMatch(student -> student.getAge() >= 18);3.2 anyMatch 检测是否存在一个或多个满足指定的参数行为,如果满足则返回true
检测是否有来自武汉大学的学生
boolean hasWhu = students.stream().anyMatch(student -> "武汉大学".equals(student.getSchool()));3.3 noneMathch 检测是否不存在满足指定行为的元素
检测是否不存在专业为‘计算机科学’的学生
boolean noneCs = students.stream().noneMatch(student -> "计算机科学".equals(student.getMajor()));3.4 findFirst 返回满足条件的第一条元素
专业为土木工程的排在第一个学生
Optional<Student> optStu = students.stream().filter(student -> "土木工程".equals(student.getMajor())).findFirst();Optional 参考这一篇:Java8新特性 – Optional类。
4、归约 对经过参数化操作后的集合进行进一步的运算,那么我们可用对集合实施归约操作
// 前面例子中的方法int totalAge = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .mapToInt(Student::getAge).sum();// 归约操作int totalAge = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .map(Student::getAge) .reduce(0, (a, b) -> a + b);// 进一步简化int totalAge2 = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .map(Student::getAge) .reduce(0, Integer::sum);// 采用无初始值的重载版本,需要注意返回OptionalOptional<Integer> totalAge = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .map(Student::getAge) .reduce(Integer::sum); // 去掉初始值
阅读全文
0 0
- java8新特性 --- stream(二)
- Java8新特性二--Stream
- java8新特性回顾(二)---stream api
- java8新特性之Stream API(二)
- Java8新特性 Stream
- JAVA8新特性--stream
- java8新特性-- stream
- Java8新特性(三)------Stream
- java8 新特性 - Stream
- Java8新特性Stream流
- JAVA8新特性 -- stream(三)
- Java8新特性——Lambda表达式(二)Stream语法详解
- java8新特性 (λ、stream 与 默认接口方法)
- java8新特性探究(三)-Stream类
- java8新特性(三):Stream流的概念
- java8新特性(四):Stream流的使用
- java8新特性(五):Stream数据收集
- java8新特性(六):Stream多线程并行数据处理
- 文章标题
- stopPropagation()和preventDefault()
- 贾跃亭被法院列入“老赖”名单;360回应监控视频被直播;小米计划在印度扩展业务丨价值早报
- C#网络编程系列文章(八)之UdpClient实现同步UDP服务器
- JavaScript实现H5游戏断线自动重连的技术
- java8新特性 --- stream(二)
- 创投日报:12月12日收录投融资项目22起
- CSDN 启动
- Ubuntu16.04下使用autotools生成Makefile
- C# DataTable数据量大,循环处理数据的时候优化速度
- Mac下PDF转EPS的方法
- 默认选中select 第一个option
- djangoReview(一)
- 简单封装的Stripe支付方法