Java8学习-- StreamAPI(3)
2018-03-13 22:34
323 查看
package com.atguigu.java8; import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import org.junit.Test; import com.atguigu.java8.Employee.Status; public class TestStreamAPI3 { List<Employee> emps = Arrays.asList( new Employee(102, "李四", 79, 6666.66, Status.BUSY), new Employee(101, "张三", 18, 9999.99, Status.FREE), new Employee(103, "王五", 28, 3333.33, Status.VOCATION), new Employee(104, "赵六", 8, 7777.77, Status.BUSY), new Employee(104, "赵六", 8, 7777.77, Status.FREE), new Employee(104, "赵六", 8, 7777.77, Status.FREE), new Employee(105, "田七", 38, 5555.55, Status.BUSY) ); //3. 终止操作 /* 归约 reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以将流中元素反复结合起来,得到一个值。 */ @Test public void test1(){ List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); Integer sum = list.stream() .reduce(0, (x, y) -> x + y); System.out.println(sum); System.out.println("----------------------------------------"); Optional<Double> op = emps.stream() .map(Employee::getSalary) .reduce(Double::sum); System.out.println(op.get()); } //需求:搜索名字中 “六” 出现的次数 @Test public void test2(){ Optional<Integer> sum = emps.stream() .map(Employee::getName) .flatMap(TestStreamAPI1::filterCharacter) .map((ch) -> { if(ch.equals('六')) return 1; else return 0; }).reduce(Integer::sum); System.out.println(sum.get()); } //collect——将流转换为其他形式。接收一个 Collector接口的实现,用于给Stream中元素做汇总的方法 @Test public void test3(){ List<String> list = emps.stream() .map(Employee::getName) .collect(Collectors.toList()); list.forEach(System.out::println); System.out.println("----------------------------------"); Set<String> set = emps.stream() .map(Employee::getName) .collect(Collectors.toSet()); set.forEach(System.out::println); System.out.println("----------------------------------"); HashSet<String> hs = emps.stream() .map(Employee::getName) .collect(Collectors.toCollection(HashSet::new)); hs.forEach(System.out::println); } @Test public void test4(){ Optional<Double> max = emps.stream() .map(Employee::getSalary) .collect(Collectors.maxBy(Double::compare)); System.out.println(max.get()); Optional<Employee> op = emps.stream() .collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))); System.out.println(op.get()); Double sum = emps.stream() .collect(Collectors.summingDouble(Employee::getSalary)); System.out.println(sum); Double avg = emps.stream() .collect(Collectors.averagingDouble(Employee::getSalary)); System.out.println(avg); Long count = emps.stream() .collect(Collectors.counting()); System.out.println(count); System.out.println("--------------------------------------------"); DoubleSummaryStatistics dss = emps.stream() .collect(Collectors.summarizingDouble(Employee::getSalary)); System.out.println(dss.getMax()); } //分组 @Test public void test5(){ Map<Status, List<Employee>> map = emps.stream() .collect(Collectors.groupingBy(Employee::getStatus)); System.out.println(map); } //多级分组 @Test public void test6(){ Map<Status, Map<String, List<Employee>>> map = emps.stream() .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> { if(e.getAge() >= 60) return "老年"; else if(e.getAge() >= 35) return "中年"; else return "成年"; }))); System.out.println(map); } //分区 @Test public void test7(){ Map<Boolean, List<Employee>> map = emps.stream() .collect(Collectors.partitioningBy((e) -> e.getSalary() >= 5000)); System.out.println(map); } // @Test public void test8(){ String str = emps.stream() .map(Employee::getName) .collect(Collectors.joining("," , "----", "----")); System.out.println(str); } @Test public void test9(){ Optional<Double> sum = emps.stream() .map(Employee::getSalary) .collect(Collectors.reducing(Double::sum)); System.out.println(sum.get()); } }
相关文章推荐
- Java 8学习之Stream API
- Java 8学习之Stream API
- Java8学习笔记之Stream API
- Java8学习笔记 — 【Stream API】
- Java8学习:Lambda表达式、Stream API和功能性接口 — 教程、资源、书籍和实例
- Java8学习笔记之Stream API
- JAVA 函数式编程学习之Stream API
- Java学习(五).API基础
- 黑马程序员Java培训、Android培训-Java 学习过程记录_JavaAPI-1
- JPA(Java Persistence API)学习总结
- RTC Java API 学习笔记
- Java-API学习笔记---Object
- JAVA高级01_09 Math与Random类 学习API的方法 2011-4-21
- 黑马程序员-学习日记-JAVA中API中的Object类和包的含义及内部类
- Java 语言的 XPath 学习 API
- hbasejava api学习
- 黑马程序员_JAVA学习日记_JAVA中API中Math和Random对象和继承1
- JAVA学习提高之----JAVA EXCEL API及JAVA 操作Excel(二)
- JAVA中API的学习先后,以及查询网址和JAVA技术论坛
- JAVA学习:maven开发环境快速搭建How to download J2EE API (javaee.jar) from Maven