result = new ArrayList<>();
102 | int valueIndex = 0;
103 | for (T value : list) {
104 |
105 | int innerIndex = 0;
106 | for (T inner : list) {
107 |
108 | if (innerIndex <= valueIndex) {
109 | innerIndex++;
110 | continue;
111 | }
112 | // 避免自己和自己比
113 | if (predicate.test(value, inner)) {
114 | return false;
115 | }
116 | innerIndex++;
117 | }
118 | valueIndex++;
119 | }
120 | return true;
121 | } else {
122 | return true;
123 | }
124 | }
125 |
126 | /**
127 | * 列表去重
128 | *
129 | * 更具对象的equal方法进行判定是否重复
130 | *
131 | * @param list
132 | * @return
133 | */
134 | public static List uniqueList(List list) {
135 | if (isNotEmpty(list)) {
136 | return list.stream().distinct().collect(Collectors.toList());
137 | } else {
138 | return list;
139 | }
140 | }
141 |
142 | /**
143 | * 对列表进行去重
144 | *
145 | * @param list
146 | * @param predicate 如果两个数据相同,返回true
147 | * @param
148 | * @return
149 | */
150 | public static List uniqueList(List list, BiPredicate predicate) {
151 | if (isNotEmpty(list)) {
152 | List result = new ArrayList<>();
153 | for (T value : list) {
154 | boolean existed = false;
155 |
156 | for (T inner : result) {
157 | // 避免自己和自己比
158 | if (value != inner && predicate.test(value, inner)) {
159 | existed = true;
160 | break;
161 | }
162 | }
163 | if (!existed) {
164 | result.add(value);
165 | }
166 | }
167 | return result;
168 | } else {
169 | return list;
170 | }
171 | }
172 |
173 | /**
174 | * 找出list1 和list2中公共的元素
175 | *
176 | * @param list1
177 | * @param list2
178 | * @param
179 | * @return
180 | */
181 | public static List commonList(List list1, List list2) {
182 | List result = new ArrayList<>();
183 | if (isEmpty(list1) || isEmpty(list2)) {
184 | return result;
185 | }
186 | list1.forEach((value) -> {
187 | if (list2.contains(value)) {
188 | result.add(value);
189 | }
190 | });
191 |
192 | return result;
193 | }
194 | /**
195 | * 找出list1 和list2中公共的元素
196 | *
197 | * @param list1
198 | * @param list2
199 | * @param
200 | * @return
201 | */
202 | public static Set commonList(Set list1, List list2){
203 |
204 | Set result = new HashSet<>();
205 | if (isEmpty(list1) || isEmpty(list2)) {
206 | return result;
207 | }
208 | list1.forEach((value) -> {
209 | if (list2.contains(value)) {
210 | result.add(value);
211 | }
212 | });
213 |
214 | return result;
215 | }
216 |
217 |
218 | /**
219 | * 将相同的字段对象,映射为同一对象列表,
220 | * [{"aa":"a","bb":"b"},{"aa":"a","bb":"bb"}]
221 | * ||
222 | * V
223 | * {"a":[{"aa":"a","bb":"b"},{"aa":"a","bb":"bb"}]}
224 | *
225 | * @param list 目标数据
226 | * @param function 计算字段的函数,
227 | * @param 泛型,归纳字段
228 | * @param 目标对象
229 | * @return 同一字段相同的,map映射
230 | * @warning 目标字段为null,数据将被抛弃
231 | */
232 | public static Map> mapSameFieldToListMap(List list, Function function) {
233 | Map> result = new HashMap<>();
234 | if (isEmpty(list)) {
235 | return result;
236 | }
237 |
238 | list.forEach((value) -> {
239 | S key = function.apply(value);
240 | if (key == null) {
241 | return;
242 | }
243 |
244 | List tList = result.get(key);
245 | if (tList == null) {
246 | tList = new ArrayList<>();
247 | tList.add(value);
248 | result.put(key, tList);
249 | } else {
250 | tList.add(value);
251 | }
252 | });
253 |
254 | return result;
255 | }
256 |
257 | /**
258 | * 将相同的字段对象,映射为同一对象列表, (并行版本,会打乱顺序)
259 | * [{"aa":"a","bb":"b"},{"aa":"a","bb":"bb"}]
260 | * ||
261 | * V
262 | * {"a":[{"aa":"a","bb":"b"},{"aa":"a","bb":"bb"}]}
263 | *
264 | * @param list 目标数据
265 | * @param function 计算字段的函数,
266 | * @param 泛型,归纳字段
267 | * @param 目标对象
268 | * @return 同一字段相同的,map映射
269 | * @warning 目标字段为null,数据将被抛弃
270 | */
271 | public static Map> mapSameFieldToListMapParallel(final List list, Function function) {
272 | Map> result = new ConcurrentHashMap<>();
273 | if (isEmpty(list)) {
274 | return result;
275 | }
276 |
277 | list.parallelStream().forEach((value) -> {
278 | S key = function.apply(value);
279 | if (key == null) {
280 | return;
281 | }
282 |
283 | List tList = result.get(key);
284 | if (tList == null) {
285 | synchronized (list){
286 | tList = result.get(key);
287 | if(tList == null){
288 | tList = Collections.synchronizedList(new ArrayList<>());
289 | }
290 | tList.add(value);
291 | result.put(key, tList);
292 | }
293 | } else {
294 | tList.add(value);
295 | }
296 | });
297 | return result;
298 | }
299 |
300 |
301 | /**
302 | * 对象列表映射为另外一个类型的列表,重复数据将被去掉
303 | *
304 | * @param list 数据列表
305 | * @param function 映射函数
306 | * @param 原
307 | * @param 目标
308 | * @return 映射后数据列表
309 | */
310 | public static List mapToList(List list, Function function) {
311 | if (isEmpty(list)) {
312 | return new ArrayList<>();
313 | }
314 | return list.stream().map(function).distinct().collect(Collectors.toList());
315 | }
316 |
317 |
318 | /**
319 | * 对象列表映射为另外一个类型的列表,重复数据将被去掉 (并行版本,会打乱顺序)
320 | *
321 | * @param list 数据列表
322 | * @param function 映射函数
323 | * @param 原
324 | * @param 目标
325 | * @return 映射后数据列表
326 | */
327 | public static List mapToListParallel(List list, Function function) {
328 | if (isEmpty(list)) {
329 | return new ArrayList<>();
330 | }
331 | return list.parallelStream().map(function).distinct().collect(Collectors.toList());
332 | }
333 |
334 |
335 | /**
336 | * 对象列表映射为另外一个类型的集合
337 | *
338 | * @param list 数据列表
339 | * @param function 映射函数
340 | * @param 原
341 | * @param 目标
342 | * @return 映射后数据集合
343 | */
344 | public static Set mapToSet(List list, Function function) {
345 | if (isEmpty(list)) {
346 | return new HashSet<>();
347 | }
348 | return list.stream().map(function).collect(Collectors.toSet());
349 | }
350 |
351 | /**
352 | * 对象列表映射为另外一个类型的集合 (并行版本,会打乱顺序)
353 | *
354 | * @param list 数据列表
355 | * @param function 映射函数
356 | * @param 原
357 | * @param 目标
358 | * @return 映射后数据集合
359 | */
360 | public static Set mapToSetParallel(List list, Function function) {
361 | if (isEmpty(list)) {
362 | return new HashSet<>();
363 | }
364 | return list.parallelStream().map(function).collect(Collectors.toSet());
365 | }
366 |
367 |
368 | /**
369 | * 对象列表映射为一个map
370 | *
371 | * @param list 数据列表
372 | * @param kFunction 获取map key的函数
373 | * @param vFunction 获取map value的函数
374 | * @param 原数据
375 | * @param key
376 | * @param value
377 | * @return 映射后数据map
378 | */
379 | public static Map mapToMap(List list, Function kFunction, Function vFunction) {
380 | if (isEmpty(list)) {
381 | return new HashMap<>();
382 | }
383 | Map result = new HashMap<>();
384 | list.forEach((value) -> {
385 | K key = kFunction.apply(value);
386 |
387 | V v = result.get(key);
388 | if (v == null) {
389 | result.put(key, vFunction.apply(value));
390 | }
391 | });
392 | return result;
393 | }
394 |
395 | /**
396 | * 对象列表映射为一个map (并行版本,会打乱顺序)
397 | *
398 | * @param list 数据列表
399 | * @param kFunction 获取map key的函数
400 | * @param vFunction 获取map value的函数
401 | * @param 原数据
402 | * @param key
403 | * @param value
404 | * @return 映射后数据map
405 | */
406 | public static Map mapToMapParallel(final List list, Function kFunction, Function vFunction) {
407 | Map result = new ConcurrentHashMap<>();
408 |
409 | if (isEmpty(list)) {
410 | return result;
411 | }
412 |
413 | list.parallelStream().forEach((value) -> {
414 | K key = kFunction.apply(value);
415 |
416 | V v = result.get(key);
417 | if (v == null) {
418 | synchronized (list){
419 | v = result.get(key);
420 | if(v == null){
421 | result.put(key, vFunction.apply(value));
422 | }
423 | }
424 | }
425 | });
426 | return result;
427 | }
428 |
429 | /**
430 | * 从一个列表中对象中,抽出一个字段列表,并去重
431 | *
432 | * @param list 列表数据
433 | * @param function 取字段函数
434 | * @param 字段类型
435 | * @param 数据类型
436 | * @return 字段列表
437 | */
438 | public static List mapGetSameFieldUniqueList(List list, Function function) {
439 | List result = new ArrayList<>();
440 | if (isEmpty(list)) {
441 | return result;
442 | }
443 | result.addAll(list.stream().map(function).filter(Objects::nonNull).distinct().collect(Collectors.toList()));
444 |
445 | return result;
446 | }
447 |
448 |
449 | /**
450 | * 从一个列表中对象中,抽出一个字段列表,并去重 (并行版本,会打乱顺序)
451 | *
452 | * @param list 列表数据
453 | * @param function 取字段函数
454 | * @param 字段类型
455 | * @param 数据类型
456 | * @return 字段列表
457 | */
458 | public static List mapGetSameFieldUniqueListParallel(List list, Function function) {
459 | List result = new ArrayList<>();
460 | if (isEmpty(list)) {
461 | return result;
462 | }
463 | result.addAll(list.parallelStream().map(function).filter(Objects::nonNull).distinct().collect(Collectors.toList()));
464 |
465 | return result;
466 | }
467 |
468 |
469 | /**
470 | * 从一个列表中对象中,抽出一个字段列表,并去重
471 | *
472 | * @param list 列表数据
473 | * @param function 取字段函数
474 | * @param uniquePredicate 判断唯一函数
475 | * @param 字段类型
476 | * @param 数据类型
477 | * @return 字段列表
478 | */
479 | public static List mapGetSameFieldUniqueList(List list, Function function, BiPredicate uniquePredicate) {
480 | List result = new ArrayList<>();
481 | if (isEmpty(list)) {
482 | return result;
483 | }
484 | result.addAll(list.stream().map(function).filter(Objects::nonNull).collect(Collectors.toList()));
485 |
486 | return uniqueList(result, uniquePredicate);
487 | }
488 |
489 |
490 | /**
491 | * 从一个列表中对象中,抽出一个字段列表,并去重 (并行版本,会打乱顺序)
492 | *
493 | * @param list 列表数据
494 | * @param function 取字段函数
495 | * @param uniquePredicate 判断唯一函数
496 | * @param 字段类型
497 | * @param 数据类型
498 | * @return 字段列表
499 | */
500 | public static List mapGetSameFieldUniqueListParallel(List list, Function function, BiPredicate uniquePredicate) {
501 | List result = new ArrayList<>();
502 | if (isEmpty(list)) {
503 | return result;
504 | }
505 | result.addAll(list.parallelStream().map(function).filter(Objects::nonNull).collect(Collectors.toList()));
506 |
507 | return uniqueList(result, uniquePredicate);
508 | }
509 |
510 | /**
511 | * 将list1 减去list2中的元素,得到的列表,可能为空
512 | *
513 | * @param list1 被减数 将不会改变
514 | * @param list2 减数 将不会改变
515 | * @param
516 | * @return list1 - list2 之后的剩余
517 | */
518 | public static List subtractList(List list1, List list2) {
519 | List result = new ArrayList<>();
520 |
521 | if (isEmpty(list1)) {
522 | return result;
523 | }
524 | if (isEmpty(list2)) {
525 | return list1;
526 | }
527 | result.addAll(list1);
528 | result.removeAll(list2);
529 | return result;
530 | }
531 |
532 | /**
533 | * 将list1 减去list2中的元素,得到的列表,可能为空
534 | *
535 | * @param list1 被减数 将不会改变
536 | * @param list2 减数 将不会改变
537 | * @param
538 | * @return list1 - list2 之后的剩余
539 | */
540 | public static Set subtractList(Set list1, Set list2) {
541 | Set result = new HashSet<>();
542 |
543 | if (isEmpty(list1)) {
544 | return result;
545 | }
546 | if (isEmpty(list2)) {
547 | return list1;
548 | }
549 | result.addAll(list1);
550 | result.removeAll(list2);
551 | return result;
552 | }
553 |
554 |
555 | /**
556 | * 合并列表,如果有重复的,最终的结果将会去重
557 | *
558 | * @param list
559 | * @param list2
560 | * @param
561 | * @return
562 | */
563 | @SafeVarargs
564 | public static List mergeList(List list, List... list2) {
565 | Assert.notNull(list, "list is null");
566 | Assert.notNull(list2, "list2 is null");
567 |
568 | List result = new ArrayList<>(list);
569 | if (list2.length > 0) {
570 | for (List ts : list2) {
571 | result.addAll(ts);
572 | }
573 | return uniqueList(result);
574 | } else {
575 | return list;
576 | }
577 | }
578 |
579 | /**
580 | * 合并列表,如果有重复的,最终的结果将会去重
581 | * @param list
582 | * @param list2
583 | * @return
584 | * @param
585 | */
586 | @SafeVarargs
587 | public static List mergeList(T[] list,T[]... list2){
588 |
589 | List result = new ArrayList<>();
590 |
591 | if(list != null && list.length != 0){
592 | result.addAll(CollectionUtils.ofList(list));
593 | }
594 |
595 | if(list2.length > 0){
596 | for (T[] tmpList : list2) {
597 |
598 | if(tmpList != null && tmpList.length != 0){
599 | result.addAll(CollectionUtils.ofList(tmpList));
600 | }
601 | }
602 | }
603 | return uniqueList(result);
604 | }
605 |
606 |
607 |
608 | /**
609 | * 取列表中的第一个
610 | *
611 | * @param list 列表数据
612 | * @param 泛型
613 | * @return 值
614 | */
615 | public static T first(List list) {
616 | if (isEmpty(list)) {
617 | return null;
618 | }
619 | return list.get(0);
620 | }
621 |
622 | /**
623 | * 取列表中的第一个
624 | *
625 | * @param list 列表数据
626 | * @param 泛型
627 | * @return 值
628 | */
629 | public static T first(T[] list) {
630 | if (isEmpty(list)) {
631 | return null;
632 | }
633 | return list[0];
634 | }
635 |
636 | /**
637 | * 快速列表列表
638 | *
639 | * @param data 数据数组
640 | * @param 数据类型
641 | * @return 列表
642 | */
643 | public static List ofList(T... data) {
644 | List result = new ArrayList<>();
645 | if (data.length > 0) {
646 | for (int i = 0; i < data.length; i++) {
647 | result.add(data[i]);
648 | }
649 | }
650 | return result;
651 | }
652 |
653 | /**
654 | * 快速列表列表
655 | *
656 | * @param data 数据数组
657 | * @param 数据类型
658 | * @return 列表
659 | */
660 | public static List ofList(Collection data) {
661 | List result = new ArrayList<>();
662 | if (data != null && data.size() > 0) {
663 | result.addAll(data);
664 | }
665 | return result;
666 | }
667 |
668 | /**
669 | * 快速set
670 | *
671 | * @param data 数据
672 | * @param 泛型
673 | * @return 集合
674 | */
675 | public static Set ofSet(T... data) {
676 | Set result = new HashSet<>();
677 | if (data.length > 0) {
678 | for (int i = 0; i < data.length; i++) {
679 | result.add(data[i]);
680 | }
681 | }
682 | return result;
683 | }
684 |
685 | /**
686 | * 快速set
687 | *
688 | * @param data 数据
689 | * @param 泛型
690 | * @return 集合
691 | */
692 | public static Set ofSet(Collection data) {
693 | Set result = new HashSet<>();
694 | if (data != null && data.size() > 0) {
695 | result.addAll(data);
696 | }
697 | return result;
698 | }
699 |
700 | /**
701 | * 反向list 最后的成员放在最前面
702 | *
703 | * @param list
704 | * @param
705 | * @return
706 | */
707 | public static List revert(List list) {
708 | List result = new ArrayList<>();
709 |
710 | if (isNotEmpty(list)) {
711 | for (int i = list.size() - 1; i > -1; i--) {
712 | result.add(list.get(i));
713 | }
714 | }
715 | return result;
716 | }
717 |
718 |
719 | }
720 |
--------------------------------------------------------------------------------