集合工具类

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;
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值