All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.CollectionUtils Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.utils;

import com.google.common.base.Function;
import com.google.common.collect.Maps;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Elon.su on 17/5/29.
 */
public class CollectionUtils{

    /**
     * 集合是否为空
     * @param coll 目标集合
     * @return 不为空返回true
     */
    public static boolean isNotEmpty(final Collection coll){
        return !isEmpty(coll);
    }

    /**
     * 集合是否为空
     * @param coll 目标集合
     * @return 为空返回true
     */
    public static boolean isEmpty(final Collection coll){
        return org.apache.commons.collections4.CollectionUtils.isEmpty(coll);
    }


    /**
     * 进行map集合中相同元素的value值聚合
     * @param target 目标Map
     * @param multis 往目标Map上追加的多个Map
     * @param  泛型约束Key
     * @param  泛型约束Map
     * @return 多个mapKey聚合后的结果, key相同的元素, value值进行合并
     */
    public static  Map> keyMerged(Map> target, Map>... multis){
        Map> base = Optional.ofNullable(target).orElse(Maps.newHashMap());

        // 待追加的多个map元素

        List>> adding = Arrays.stream(multis)
                .map(x -> {
                    return Optional.ofNullable(x).orElse(Maps.newHashMap());
                })
                .collect(Collectors.toList());

        // 逐个Map元素追加
        adding.forEach(x ->{
            x.keySet().forEach(
                    key -> base.merge(key, x.get(key), (v1, v2)
                            -> Arrays.asList(v1, v2).stream().flatMap(Collection::stream).collect(Collectors.toList())
                    )
            );
        });

        return base;
    }


    /**
     * 对list元素进行转化。
     * @param srcList   源list
     * @param fun       转换函数
     * @param        源类型
     * @param        目标类型
     * @return          转换后的list
     */
    public static  List transform(Collection srcList, Function fun) {
        if (isEmpty(srcList)) {
            return Collections.EMPTY_LIST;
        }
        List descList = new ArrayList<>(srcList.size());
        srcList.forEach(x -> {
            descList.add(fun.apply(x));
        });
        return descList;
    }

    /**
     * Map按照Value进行排期
     * @param map 目标map
     * @param  key元素
     * @param  value元素
     * @return 排序后的结果
     */
    public static > Map sortByValue(Map map) {
        List> list = new LinkedList>(map.entrySet());
        Collections.sort(list, new Comparator>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        Map result = new LinkedHashMap();
        for (Map.Entry entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
}