Stream的终止操作
* allMatch 是否匹配所有
* anyMatch 是否匹配一个
* noneMatch 是否没有匹配一个
* findFirst 返回第一个
* count 返回总数
* max 返回最大
* min 返回最小
* reduce 归约 将元素连续操作得到结果
* collect 收集
1 将流转换成其他的形式(其他集合)
2 接收Collector接口 用于Stream中元素各种汇总 特例 Collectors.joining()拼接字符串
Optional 类主要解决的问题是臭名昭著的空指针异常(NullPointerException)
test07结果
test08结果
collect 是Stream接口中的一个 方法
Collector是一个接口
Collector是专门作为collect方法的参数
Collectors是一个工具类
生成具体的Collector
1 package com.wf.zhang.java8.stream; 2 3 import com.wf.zhang.java8.lamdba.Employee; 4 import org.junit.Test; 5 6 import java.util.*; 7 import java.util.stream.Collectors; 8 9 public class TestStream3 { 10 //准备数据 11 List<Employee> list = Arrays.asList( 12 new Employee("张飞", 18, 5000.00, Employee.Status.FREE), 13 new Employee("赵云", 28, 6666.66,Employee.Status.BUSY), 14 new Employee("关羽", 38, 7777.77,Employee.Status.VOCATION), 15 new Employee("曹阿蛮", 29, 7777.77,Employee.Status.FREE), 16 new Employee("刘备", 48, 10000.88,Employee.Status.BUSY), 17 new Employee("刘备", 48, 10000.88,Employee.Status.BUSY), 18 new Employee("刘备", 48, 10000.88,Employee.Status.BUSY), 19 new Employee("诸葛亮", 26, 10000.88,Employee.Status.FREE), 20 new Employee("诸葛亮", 26, 10000.88,Employee.Status.FREE), 21 new Employee("诸葛亮", 26, 10000.88,Employee.Status.FREE) 22 ); 23 24 /*** 25 * Stream的终止操作 26 * allMatch 是否匹配所有 27 * anyMatch 是否匹配一个 28 * noneMatch 是否没有匹配一个 29 * findFirst 返回第一个 30 * count 返回总数 31 * max 返回最大 32 * min 返回最小 33 * reduce 归约 将元素连续操作得到结果 34 * collect 收集 1 将流转换成其他的形式(其他集合) 35 * 2 接收Collector接口 用于Stream中元素各种汇总 特例Collectors.joining()拼接字符串 36 */ 37 38 //allMatch 是否匹配所有 39 @Test 40 public void test01(){ 41 42 boolean b = list.stream() 43 .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); 44 System.out.println(b); 45 } 46 47 //anyMatch 是否匹配一个 48 @Test 49 public void test02(){ 50 51 boolean b = list.stream() 52 .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); 53 System.out.println(b); 54 } 55 56 //noneMatch 是否没有匹配一个 57 @Test 58 public void test03(){ 59 60 boolean b = list.stream() 61 .noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY)); 62 System.out.println(b); 63 } 64 65 //findFirst 返回第一个 66 @Test 67 public void test04(){ 68 69 Optional<Employee> op = list.stream() 70 .sorted(Comparator.comparingDouble(Employee::getSalary)) 71 .findFirst(); 72 System.out.println(op.get()); 73 } 74 75 //count 返回总数 76 //max 返回最大 77 //min 返回最小 78 @Test 79 public void test06(){ 80 81 long count = list.stream() 82 .count(); 83 84 System.out.println(count); 85 86 87 Optional<Employee> op = list.stream() 88 .max(Comparator.comparingDouble(Employee::getSalary)); 89 90 System.out.println(op); 91 92 93 Optional<Double> op2 = list.stream() 94 .map(Employee::getSalary) 95 .min(Double::compareTo); 96 97 System.out.println(op2); 98 } 99 100 //reduce 归约 将元素连续操作得到结果 101 @Test 102 public void test07(){ 103 104 //从0开始 连续相加的和 105 List<Integer> newlist = Arrays.asList(1,1,6,7,8,9,3); 106 Integer sum = newlist.stream() 107 .reduce(0, (x, y) -> x + y); 108 System.out.println(sum); 109 System.out.println("===============从0开始 连续相加的和==================="); 110 //计算工资总和 111 Optional<Double> rs = list.stream() 112 .map(Employee::getSalary) 113 .reduce(Double::sum); 114 System.out.println(rs); 115 System.out.println("===============计算工资总和==================="); 116 117 } 118 119 //collect 收集 1 将流转换成其他的形式(其他集合) 120 // 2 接收Collector接口 用于Stream中元素各种汇总 121 @Test 122 public void test08() { 123 124 //收集到list集合 125 List<String> collect = list.stream() 126 .map(Employee::getName) 127 .collect(Collectors.toList()); 128 129 collect.forEach(System.out::println); 130 131 System.out.println("===============收集到list集合==================="); 132 133 //收集到set集合 去重复 134 Set<String> set = list.stream() 135 .map(Employee::getName) 136 .collect(Collectors.toSet()); 137 138 set.forEach(System.out::println); 139 140 System.out.println("===============收集到set集合 去重复==================="); 141 142 //收集到HashSet集合 143 HashSet<String> hashSet = list.stream() 144 .map(Employee::getName) 145 .collect(Collectors.toCollection(HashSet::new)); 146 147 hashSet.forEach(System.out::println); 148 149 System.out.println("===============收集到HashSet集合==================="); 150 151 //总数 可以用list.stream().count();代替 152 Long su = list.stream() 153 .collect(Collectors.counting()); 154 System.out.println(su); 155 156 System.out.println("=================总数================="); 157 158 //平均工资 159 Double avg = list.stream() 160 .collect(Collectors.averagingDouble(Employee::getSalary)); 161 System.out.println(avg); 162 163 System.out.println("=================avg平均工资================="); 164 165 //工资总和 166 DoubleSummaryStatistics sum2 = list.stream() 167 .collect(Collectors.summarizingDouble(Employee::getSalary)); 168 System.out.println(sum2); 169 170 171 System.out.println("================工资总和=================="); 172 173 //分组 174 Map<String, List<Employee>> map = list.stream() 175 .collect(Collectors.groupingBy(Employee::getName)); 176 System.out.println(map); 177 178 System.out.println("================分组=================="); 179 180 //多次分组 181 Map<Employee.Status, Map<String, List<Employee>>> map2 = list.stream() 182 .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> { 183 if (e.getAge() <= 35) { 184 return "青年"; 185 } else if (e.getAge() <= 50) { 186 return "中年"; 187 } else { 188 return "老年"; 189 } 190 }))); 191 System.out.println(map2); 192 193 System.out.println("============多次分组======================"); 194 195 //分区 false / true 196 Map<Boolean, List<Employee>> map3 = list.stream() 197 .collect(Collectors.partitioningBy((e) -> e.getSalary() > 8000)); 198 System.out.println(map3); 199 200 System.out.println("================分区 false / true=================="); 201 202 //另一种计算 203 DoubleSummaryStatistics d = list.stream() 204 .collect(Collectors.summarizingDouble(Employee::getSalary)); 205 206 System.out.println("最大"+d.getMax()); 207 System.out.println("平均值"+d.getAverage()); 208 System.out.println("总数"+d.getCount()); 209 System.out.println("最小"+d.getMin()); 210 System.out.println("总和"+d.getSum()); 211 212 System.out.println("================另一种计算=================="); 213 //拼接字符串 214 String s = list.stream() 215 .map(Employee::getName) 216 .collect(Collectors.joining(",")); 217 System.out.println(s); 218 System.out.println("================拼接字符串=================="); 219 } 220 }
1 package com.wf.zhang.java8.lamdba; 2 3 public class Employee { 4 private int id; 5 private String name; 6 private int age; 7 private double salary; 8 private Status status; 9 10 public Employee() { 11 } 12 13 public Employee(String name) { 14 this.name = name; 15 } 16 17 18 public Employee( int age) { 19 this.age = age; 20 } 21 public Employee(String name, int age) { 22 this.name = name; 23 this.age = age; 24 } 25 public Employee(int id, int age) { 26 this.id = id; 27 this.age = age; 28 } 29 30 public Employee(String name, int age, double salary) { 31 32 this.name = name; 33 this.age = age; 34 this.salary = salary; 35 } 36 public Employee(int id, String name, int age, double salary) { 37 this.id = id; 38 this.name = name; 39 this.age = age; 40 this.salary = salary; 41 } 42 43 public Employee(String name, int age, double salary, Status status) { 44 this.name = name; 45 this.age = age; 46 this.salary = salary; 47 this.status = status; 48 } 49 public Employee(int id, String name, int age, double salary, Status status) { 50 this.id = id; 51 this.name = name; 52 this.age = age; 53 this.salary = salary; 54 this.status = status; 55 } 56 57 public Status getStatus() { 58 return status; 59 } 60 61 public void setStatus(Status status) { 62 this.status = status; 63 } 64 65 public int getId() { 66 return id; 67 } 68 69 public void setId(int id) { 70 this.id = id; 71 } 72 73 public String getName() { 74 return name; 75 } 76 77 public void setName(String name) { 78 this.name = name; 79 } 80 81 public int getAge() { 82 return age; 83 } 84 85 public void setAge(int age) { 86 this.age = age; 87 } 88 89 public double getSalary() { 90 return salary; 91 } 92 93 public void setSalary(double salary) { 94 this.salary = salary; 95 } 96 97 public String show() { 98 return "测试方法引用!"; 99 } 100 101 @Override 102 public int hashCode() { 103 final int prime = 31; 104 int result = 1; 105 result = prime * result + age; 106 result = prime * result + id; 107 result = prime * result + ((name == null) ? 0 : name.hashCode()); 108 long temp; 109 temp = Double.doubleToLongBits(salary); 110 result = prime * result + (int) (temp ^ (temp >>> 32)); 111 return result; 112 } 113 114 @Override 115 public boolean equals(Object obj) { 116 if (this == obj) 117 return true; 118 if (obj == null) 119 return false; 120 if (getClass() != obj.getClass()) 121 return false; 122 Employee other = (Employee) obj; 123 if (age != other.age) 124 return false; 125 if (id != other.id) 126 return false; 127 if (name == null) { 128 if (other.name != null) 129 return false; 130 } else if (!name.equals(other.name)) 131 return false; 132 if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary)) 133 return false; 134 return true; 135 } 136 137 @Override 138 public String toString() { 139 return "Employee [ name=" + name + ", age=" + age + ", salary=" + salary + ", status=" + status 140 + "]"; 141 } 142 143 public enum Status { 144 FREE, BUSY, VOCATION; 145 } 146 147 }