package com.nuzar.cloud.common.utils;
import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author MadMax
* @date 2021/6/23 11:14
*/
public class CollectionUtils extends CollectionUtil {
/**
* 校验list中是否有重复项
*
* @param list
* @param fnKey
* @param <T>
* @return
*/
public static <T> boolean checkDuplicate(List<T> list, Function<? super T, Object> fnKey) {
if (list == null || list.size() == 0) {
return false;
}
long count = list.stream().map(fnKey).distinct().count();
return count - list.size() != 0;
}
/**
* 差集 返回collection
*
* @return
*/
public static <T> Collection<T> getDefetchCollection(Collection<T> collection1,
Collection<T> collection2,
Function<? super T, Object> fnKey) {
if (collection1 == null || collection1.size() == 0) {
return collection1;
}
if (collection2 == null || collection2.size() == 0) {
return collection1;
}
return collection1.stream().
filter(
t -> collection2.stream().map(fnKey).noneMatch(k -> k != null
&& k.equals(fnKey.apply(t)))
)
.collect(Collectors.toList());
}
/**
* 差集 返回list
*
* @return
*/
public static <T> List<T> getDefetchCollection(List<T> collection1,
List<T> collection2,
Function<? super T, Object> fnKey) {
if (collection1 == null || collection1.size() == 0) {
return collection1;
}
if (collection2 == null || collection2.size() == 0) {
return collection1;
}
return collection1.stream().
filter(
t -> collection2.stream().map(fnKey).noneMatch(k -> k != null
&& k.equals(fnKey.apply(t)))
)
.collect(Collectors.toList());
}
/**
* 交集
*
* @return
*/
public static <T> Collection<T> getIntersection(Collection<T> collection1,
Collection<T> collection2,
Function<T, Object> fnKey) {
if (collection1 == null || collection1.size() == 0
|| collection2 == null || collection2.size() == 0) {
return null;
}
return collection1.stream().filter(t -> collection2.stream().anyMatch(c -> fnKey.apply(t) != null &&
fnKey.apply(t).equals(fnKey.apply(c)))).collect(Collectors.toList());
}
/**
* 交集
*
* @return
*/
public static <T> List<T> getIntersection(List<T> collection1,
List<T> collection2,
Function<T, Object> fnKey) {
if (collection1 == null || collection1.size() == 0
|| collection2 == null || collection2.size() == 0) {
return null;
}
return collection1.stream().filter(t -> collection2.stream().anyMatch(c -> fnKey.apply(t) != null &&
fnKey.apply(t).equals(fnKey.apply(c)))).collect(Collectors.toList());
}
/**
* 并集(去重)
*
* @return
*/
public static <T> Collection<T> getUnionCollection(Collection<T> collection1,
Collection<T> collection2,
Function<T, Object> fnKey) {
return null;
}
/**
* 如果为null 则使用function生成一个返回
*
* @param collection
* @param c2
* @param <T>
* @return
*/
public static <T> Collection<T> ifNull(Collection<T> collection, Collection<T> c2) {
return collection == null ? c2 : collection;
}
/**
* 如果为null 则使用function生成一个返回
*
* @param collection
* @param list
* @param <T>
* @return
*/
public static <T> List<T> ifNull(List<T> collection, List<T> list) {
return collection == null ? list : collection;
}
/**
* 排序
* 和sort方法区分 sort 返回一个新的列表
*
* @param collection
* @param comparable
* @param <T>
* @return
*/
public static <T> List<T> sortOnce(List<T> collection, Comparator<T> comparable) {
if (collection != null && collection.size() > 0) {
collection.sort(comparable);
}
return collection;
}
/**
* 排序 DEEP
* 父子类排序
*
* @param collection
* @param comparable
* @param <T>
* @return
*/
public static <T> List<T> sortDeep(List<T> collection, Comparator<T> comparable,
Function<T, List<T>> fnSonListGet) {
if (collection != null && collection.size() > 0) {
collection.forEach(t -> {
sortDeep(fnSonListGet.apply(t), comparable, fnSonListGet);
});
}
return sortOnce(collection, comparable);
}
public static Map sortMap(Map<String, List<Object>> map) {
List<Node> list = new ArrayList();
map.keySet().forEach(t -> {
Node node = new Node();
node.setKey(t);
node.setNum(map.get(t) != null ? map.get(t).size() : 0);
list.add(node);
});
list.sort(Comparator.comparingInt(Node::getNum));
Map newMap = new LinkedHashMap();
list.forEach(t -> {
newMap.put(t.getKey(), map.get(t.getKey()));
});
return newMap;
}
@Data
private static class Node {
private String key;
private Integer num;
}
}