写在前面:因为公司代码的List操作大多数用到了lambda表达式,这种代码写起来也比较简洁。自己闲着将一些常用的方法封装了一下,例如有过滤,去重,求和,分组还有收集,用起来也比较方便了。可以复制粘贴直接使用,其它的操作有用到后面会慢慢补充,觉得不错的大爷们可以点个????
复制代码
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480/** * <p> * JDK 8 lambda常用工具类 * </p> * * @author lzy * @since 2021-01-22 */ public class LambdaUtils { /** * list分组操作 * * @param list:要操作的list集合 * @param function:需要进行分组的对象属性 * @return Map<K, List < V>> */ public static <K, V> Map<K, List<V>> listGroupingBy(List<V> list, Function<V, K> function) { return list.stream().collect(Collectors.groupingBy(function)); } /** * list根据对象指定属性进行转Map * * @param list:要操作的list集合 * @param function:转成Map后的key 注意的是:如果有相同的key,则保留key1,key2,则保留key1舍弃key2 * @return Map<K, V> */ public static <K, V> Map<K, V> listToMap(List<V> list, Function<V, K> function) { return list.stream().collect(Collectors.toMap(function, a -> a, (k1, k2) -> k1)); } /** * list根据对象指定属性+条件过滤出新的List * * @param list:要操作的list集合 * @param predicate: 过滤条件,例如a->a.getUserName().equals("xxx) * 过滤出名字=xxx的集合 * @return List<T> */ public static <T> List<T> listFilter(List<T> list, Predicate<? super T> predicate) { return list.stream().filter(predicate).collect(toList()); } /** * list根据对象指定属性满足指定条件并返回布尔值(任意匹配到一个) * * @param list:要操作的list集合 * @param predicate: 过滤条件,例如a->a.getUserName().equals("xxx) * 过滤出名字=xxx的集合 * @return List<T> */ public static <T> boolean listAnyMatch(List<T> list, Predicate<? super T> predicate) { return list.stream().anyMatch(predicate); } /** * list根据对象指定属性满足指定条件并返回布尔值(没有匹配到一个) * * @param list:要操作的list集合 * @param predicate: 过滤条件,例如a->a.getUserName().equals("xxx) * 过滤出名字=xxx的集合 * @return List<T> */ public static <T> boolean listNoMatch(List<T> list, Predicate<? super T> predicate) { return list.stream().noneMatch(predicate); } /** * list根据对象收集出新的list * * @param list :要操作的list集合 * @param mapper : 收集 * @return List<T> */ public static <T, R, A> List<R> listToList(List<T> list, Function<? super T, ? extends R> mapper) { return list.stream().map(mapper).collect(toList()); } /** * list根据对象指定属性去重 * * @param list:要操作的list集合 * @param keyExtractor: 去重属性 * @return List<T> */ public static <T> List<T> listDistinctBy(List<T> list, Function<? super T, ?> keyExtractor) { return list.stream() .filter(distinctByKey(keyExtractor)) .collect(toList()); } //jdk 8集合根据属性去重工具 private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } /** * list去重(适用于简单包装的类型,如String,Integer..) * * @param list:要操作的list集合 * @return List<T> */ public static <T> List<T> listDistinct(List<T> list) { return list.stream().distinct().collect(toList()); } /** * list求和 (int求和) * * @param list:要操作的list集合 * @return List<T> */ public static <T> int listToSumIntBy(List<T> list, ToIntFunction<? super T> mapper) { return list.stream().mapToInt(mapper).sum(); } /** * list求和 (int求平均值) * * @param list:要操作的list集合(size不能为0,否则返回0) * @return List<T> */ public static <T> double listToIntAvgBy(List<T> list, ToIntFunction<? super T> mapper) { boolean present = list.stream().mapToInt(mapper).average().isPresent(); double asDouble = 0.0; if (present) { asDouble = list.stream().mapToInt(mapper).average().getAsDouble(); } return asDouble; } /** * list求和 (int求最大值) * * @param list:要操作的list集合(size不能为0,否则返回0) * @return List<T> */ public static <T> int listToMaxIntBy(List<T> list, ToIntFunction<? super T> mapper) { boolean present = list.stream().mapToInt(mapper).max().isPresent(); int asInt = 0; if (present) { asInt = list.stream().mapToInt(mapper).max().getAsInt(); return asInt; } return 0; } /** * list求和 (int求最小值) * * @param list:要操作的list集合(size不能为0,否则返回0) * @return List<T> */ public static <T> int listToMinIntBy(List<T> list, ToIntFunction<? super T> mapper) { boolean present = list.stream().mapToInt(mapper).min().isPresent(); int asInt = 0; if (present) { asInt = list.stream().mapToInt(mapper).min().getAsInt(); return asInt; } return 0; } /** * list转换String 用逗号分割 * * @param list:要操作的list集合(size不能为0,否则返回0) * @return List<T> */ public static <T, R, A> String listToStringComma(List<T> list, Function<? super T, ? extends R> mapper) { return StringUtils.join(list.stream().map(mapper).collect(toList()), ","); } /** * String转换list 用逗号分割 * * @param data:操作数据 如:A=1,2,3 A= A,B,A * @return List<T> */ public static <T> List<String> StringTolistComma(String data) { return Arrays.asList(StringUtils.split(data,",")).stream().map(s -> (s.trim())).collect(toList()); } /** * double 类型的集合求和 * * @param doubles * @return */ public static Double doubleSum(List<Double> doubles) { return doubles.stream().collect(Collectors.summarizingDouble(value -> value)).getSum(); } /** * 两个字符串List 取交集 * * @param listOne * @param listTwo * @return */ public static List<String> intersectionListString(List<String> listOne, List<String> listTwo) { return listOne.stream().filter(item -> listTwo.contains(item)).collect(Collectors.toList()); } /** * 两个字符串List 差集 (list1 - list2) * * @param listOne * @param listTwo * @return */ public static List<String> subtractionListString(List<String> listOne, List<String> listTwo) { return listOne.stream().filter(item -> !listTwo.contains(item)).collect(Collectors.toList()); } /** * List 并集 * * @param listOne * @param listTwo * @return */ public static List<String> unionListString(List<String> listOne, List<String> listTwo) { List<String> listAll = listOne.parallelStream().collect(toList()); List<String> listAll2 = listTwo.parallelStream().collect(toList()); listAll.addAll(listAll2); return listAll; } /** * 两个整形List 取交集 * * @param listOne * @param listTwo * @return */ public static List<Integer> intersectionListInt(List<Integer> listOne, List<Integer> listTwo) { return listOne.stream().filter(item -> listTwo.contains(item)).collect(Collectors.toList()); } /** * List int 并集 * * @param listOne * @param listTwo * @return */ public static List<Integer> unionListInt(List<Integer> listOne, List<Integer> listTwo) { List<Integer> listAll = listOne.parallelStream().collect(toList()); List<Integer> listAll2 = listTwo.parallelStream().collect(toList()); listAll.addAll(listAll2); //List->Set Set<Integer> sortSet = new LinkedHashSet<>(listAll); //Set->List return new ArrayList<>(sortSet); } /** * 截取字符串,可以替代list.subList[start,end) * * @param list 要截取的List * @param startIndex 开始位置 * @param endIndex 结束位置 * @param <T> 返回类型 * @return 截取后结果 */ public static <T> List<T> subList(List<T> list, int startIndex, int endIndex) { return list.stream() .skip(startIndex) .limit(endIndex - startIndex) .collect(Collectors.toList()); } /** 相同属性的会复制过去 * 从List<A> copy到List<B> * @param list List<B> * @param clazz B * @return List<B> */ public static <T> List<T> listCopyToList(List<?> list,Class<T> clazz){ String oldOb = JSON.toJSONString(list); return JSON.parseArray(oldOb, clazz); } /** * 从对象A copy到 对象B * @param ob A * @param clazz B.class * @return B */ public static <T> T objectCopyObj(Object ob,Class<T> clazz){ String oldOb = JSON.toJSONString(ob); return JSON.parseObject(oldOb, clazz); } //测试 public static void main(String[] args) { String string = "12gg3"; } //开始--------------------------------BigDecimal常见计算,不建议使用double和float参与计算,存在精度问题-------------------------- //内部使用函数式接口 @FunctionalInterface public interface ToBigDecimalFunction<T> { BigDecimal applyAsBigDecimal(T value); } static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet(); @SuppressWarnings("unchecked") private static <I, R> Function<I, R> castingIdentity() { return i -> (R) i; } static class CollectorImpl<T, A, R> implements Collector<T, A, R> { private final Supplier<A> supplier; private final BiConsumer<A, T> accumulator; private final BinaryOperator<A> combiner; private final Function<A, R> finisher; private final Set<Characteristics> characteristics; CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Function<A, R> finisher, Set<Characteristics> characteristics) { this.supplier = supplier; this.accumulator = accumulator; this.combiner = combiner; this.finisher = finisher; this.characteristics = characteristics; } CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Set<Characteristics> characteristics) { this(supplier, accumulator, combiner, castingIdentity(), characteristics); } @Override public BiConsumer<A, T> accumulator() { return accumulator; } @Override public Supplier<A> supplier() { return supplier; } @Override public BinaryOperator<A> combiner() { return combiner; } @Override public Function<A, R> finisher() { return finisher; } @Override public Set<Characteristics> characteristics() { return characteristics; } } //求和方法 private static <T> Collector<T, ?, BigDecimal> summingBigDecimal(com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper) { return new LambdaUtils.CollectorImpl<>( () -> new BigDecimal[]{BigDecimal.ZERO}, (a, t) -> { a[0] = a[0].add(mapper.applyAsBigDecimal(t)); }, (a, b) -> { a[0] = a[0].add(b[0]); return a; }, a -> a[0], CH_NOID); } //求最大值 private static <T> Collector<T, ?, BigDecimal> maxBy(com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper) { return new LambdaUtils.CollectorImpl<>( () -> new BigDecimal[]{new BigDecimal(Long.MIN_VALUE)}, (a, t) -> { a[0] = a[0].max(mapper.applyAsBigDecimal(t)); }, (a, b) -> { a[0] = a[0].max(b[0]); return a; }, a -> a[0], CH_NOID); } //求最小值 private static <T> Collector<T, ?, BigDecimal> minBy(com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper) { return new LambdaUtils.CollectorImpl<>( () -> new BigDecimal[]{new BigDecimal(Long.MAX_VALUE)}, (a, t) -> { a[0] = a[0].min(mapper.applyAsBigDecimal(t)); }, (a, b) -> { a[0] = a[0].min(b[0]); return a; }, a -> a[0], CH_NOID); } //求平均值 private static <T> Collector<T, ?, BigDecimal> averagingBigDecimal(com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) { return new LambdaUtils.CollectorImpl<>( () -> new BigDecimal[]{BigDecimal.ZERO, BigDecimal.ZERO}, (a, t) -> { a[0] = a[0].add(mapper.applyAsBigDecimal(t)); a[1] = a[1].add(BigDecimal.ONE); }, (a, b) -> { a[0] = a[0].add(b[0]); return a; }, a -> a[0].divide(a[1], BigDecimal.ROUND_HALF_UP).setScale(newScale, roundingMode), CH_NOID); } /** * list去重 (BigDecimal求和) * * @param list:要操作的list集合 * @param newScale: 保留小数位 * @param roundingMode : 舍弃规则 * @return List<T> */ public static <T> BigDecimal listToSumBigDecimalBy(List<T> list, com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) { return list.stream().collect(LambdaUtils.summingBigDecimal(mapper)).setScale(newScale, roundingMode); } /** * list去重 (BigDecimal求平均值) * * @param list:要操作的list集合 * @param newScale: 保留小数位 * @param roundingMode : 舍弃规则 * @return List<T> */ public static <T> BigDecimal listToAveragingBigDecimalBy(List<T> list, com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) { return list.stream().collect(LambdaUtils.averagingBigDecimal(mapper, newScale, roundingMode)); } /** * list去重 (BigDecimal求大值) * * @param list:要操作的list集合 * @param newScale: 保留小数位 * @param roundingMode : 舍弃规则 * @return List<T> */ public static <T> BigDecimal listToMaxBigDecimalBy(List<T> list, com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) { return list.stream().collect(LambdaUtils.maxBy(mapper)).setScale(newScale, roundingMode); } /** * list去重 (BigDecimal求最小值) * * @param list:要操作的list集合 * @param newScale: 保留小数位 * @param roundingMode : 舍弃规则 * @return List<T> */ public static <T> BigDecimal listToMinBigDecimalBy(List<T> list, com.hgfzp.textile.common.utils.lambda.LambdaUtils.ToBigDecimalFunction<? super T> mapper, int newScale, int roundingMode) { return list.stream().collect(LambdaUtils.maxBy(mapper)).setScale(newScale, roundingMode); } //结束------------------------------------------------------- }
最后
以上就是高兴鸵鸟最近收集整理的关于JDK 8使用Lambda操作List工具类的全部内容,更多相关JDK内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复