本文主要讲解:Java 8 Stream之Collectors.groupingBy()分组示例
功能代码:
1 2 3 4 5 6 7 8 9 |
/** * 使用java8 stream groupingBy操作,按城市分组list */ public void groupingByCity() { Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity)); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 |
/** * 使用java8 stream groupingBy操作,按城市分组list统计count */ public void groupingByCount() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.counting())); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 |
/** * 使用java8 stream groupingBy操作,按城市分组list并计算分组年龄平均值 */ public void groupingByAverage() { Map<String, Double> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getAge))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * 使用java8 stream groupingBy操作,按城市分组list并计算分组销售总值 */ public void groupingBySum() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); // 对Map按照分组销售总值逆序排序 Map<String, Long> sortedMap = new LinkedHashMap<>(); map.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed()) .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue())); sortedMap.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 |
/** * 使用java8 stream groupingBy操作,按城市分组list并通过join操作连接分组list中的对象的name 属性使用逗号分隔 */ public void groupingByString() { Map<String, String> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.joining(", ", "Names: [", "]")))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的List */ public void groupingByList() { Map<String, List<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toList()))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.stream().forEach(item -> { System.out.println("item = " + item); }); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的Set */ public void groupingBySet() { Map<String, Set<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toSet()))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.stream().forEach(item -> { System.out.println("item = " + item); }); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * 使用java8 stream groupingBy操作,通过Object对象的成员分组List */ public void groupingByObject() { Map<Manage, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(item -> { return new Manage(item.getName()); })); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } |
功能代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 使用java8 stream groupingBy操作, 基于city 和name 实现多次分组 */ public void groupingBys() { Map<String, Map<String, List<Employee>>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.forEach((i, j) -> { System.out.println(i + " = " + j); }); }); } |
功能代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 使用java8 stream groupingBy操作, 基于Distinct 去重数据 */ public void groupingByDistinct() { List<Employee> list = employees.stream().filter(distinctByKey(Employee :: getCity)) .collect(Collectors.toList());; list.stream().forEach(item->{ System.out.println("city = " + item.getCity()); }); } /** * 自定义重复key 规则 * @param keyExtractor * @return */ private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } |
完整源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
package com.stream; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; /** * Java 8 Stream 之groupingBy 分组讲解 * * @author zzg * */ public class Java8GroupBy { List<Employee> employees = new ArrayList<Employee>(); /** * 数据初始化 */ public void init() { List<String> citys = Arrays.asList("湖南", "湖北", "江西", "广西 "); for (int i = 0; i < 10; i++) { Random random = new Random(); Integer index = random.nextInt(4); Employee employee = new Employee(citys.get(index), "姓名" + i, (random.nextInt(4) * 10 - random.nextInt(4)), (random.nextInt(4) * 1000 - random.nextInt(4))); employees.add(employee); } } /** * 使用java8 stream groupingBy操作,按城市分组list */ public void groupingByCity() { Map<String, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity)); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作,按城市分组list统计count */ public void groupingByCount() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.counting())); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作,按城市分组list并计算分组年龄平均值 */ public void groupingByAverage() { Map<String, Double> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.averagingInt(Employee::getAge))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作,按城市分组list并计算分组销售总值 */ public void groupingBySum() { Map<String, Long> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.summingLong(Employee::getAmount))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); // 对Map按照分组销售总值逆序排序 Map<String, Long> sortedMap = new LinkedHashMap<>(); map.entrySet().stream().sorted(Map.Entry.<String, Long> comparingByValue().reversed()) .forEachOrdered(e -> sortedMap.put(e.getKey(), e.getValue())); sortedMap.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作,按城市分组list并通过join操作连接分组list中的对象的name 属性使用逗号分隔 */ public void groupingByString() { Map<String, String> map = employees.stream().collect(Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.joining(", ", "Names: [", "]")))); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的List */ public void groupingByList() { Map<String, List<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toList()))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.stream().forEach(item -> { System.out.println("item = " + item); }); }); } /** * 使用java8 stream groupingBy操作,按城市分组list,将List转化为name的Set */ public void groupingBySet() { Map<String, Set<String>> map = employees.stream().collect( Collectors.groupingBy(Employee::getCity, Collectors.mapping(Employee::getName, Collectors.toSet()))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.stream().forEach(item -> { System.out.println("item = " + item); }); }); } /** * 使用java8 stream groupingBy操作,通过Object对象的成员分组List */ public void groupingByObject() { Map<Manage, List<Employee>> map = employees.stream().collect(Collectors.groupingBy(item -> { return new Manage(item.getName()); })); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); } /** * 使用java8 stream groupingBy操作, 基于city 和name 实现多次分组 */ public void groupingBys() { Map<String, Map<String, List<Employee>>> map = employees.stream() .collect(Collectors.groupingBy(Employee::getCity, Collectors.groupingBy(Employee::getName))); map.forEach((k, v) -> { System.out.println(k + " = " + v); v.forEach((i, j) -> { System.out.println(i + " = " + j); }); }); } /** * 使用java8 stream groupingBy操作, 基于Distinct 去重数据 */ public void groupingByDistinct() { List<Employee> list = employees.stream().filter(distinctByKey(Employee :: getCity)) .collect(Collectors.toList());; list.stream().forEach(item->{ System.out.println("city = " + item.getCity()); }); } /** * 自定义重复key 规则 * @param keyExtractor * @return */ private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } public static void main(String[] args) { // TODO Auto-generated method stub Java8GroupBy instance = new Java8GroupBy(); instance.init(); instance.groupingByCity(); instance.groupingByCount(); instance.groupingByAverage(); instance.groupingBySum(); instance.groupingByString(); instance.groupingByList(); instance.groupingBySet(); instance.groupingByObject(); instance.groupingBys(); instance.groupingByDistinct(); } class Employee { private String city; private String name; private Integer age; private Integer amount; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } public Employee(String city, String name, Integer age, Integer amount) { super(); this.city = city; this.name = name; this.age = age; this.amount = amount; } public Employee() { super(); } } class Manage { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Manage(String name) { super(); this.name = name; } public Manage() { super(); } } } |
github 地址: 待补全
本文参考:
Java8 Stream groupingBy对List进行分组
from:https://blog.csdn.net/zhouzhiwengang/article/details/112319054