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

top.jfunc.common.utils.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 1.8.5
Show newest version
package top.jfunc.common.utils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.RandomAccess;

/**
 * @author xiongshiyan at 2019/5/21 , contact me with email [email protected] or phone 15208384257
 */
public class CollectionUtil {
    private CollectionUtil(){}

    /**
     * 判断一个集合是否为空
     * @param c 待判断集合
     * @return true if null or 0==size()
     */
    public static  boolean isEmpty(Collection c){
        return null == c || 0 == c.size();
    }
    /**
     * 判断一个集合是否非空
     * @param c 待判断集合
     * @return true if not null && size()>0
     */
    public static  boolean isNotEmpty(Collection c){
        return !isEmpty(c);
    }
    /**
     * 检查某个值是否在集合中
     */
    public static  boolean targetInCollection(T target, Collection collection) {
        if(isEmpty(collection)){
            return false;
        }

        /**
         * 如果是类似ArrayList这种可以随机访问的,使用这种方式提高效率
         * @see List
         * @see RandomAccess
         */
        if(collection instanceof List && collection instanceof RandomAccess){
            int size = collection.size();
            List list = (List) collection;
            for (int i = 0; i < size; i++) {
                if(Objects.equals(list.get(i) , target)){
                    return true;
                }
            }
            return false;
        }

        //普通的集合遍历
        for (T s : collection) {
            if(Objects.equals(s , target)){
                return true;
            }
        }
        return false;
    }
    /**
     * 合并两个集合
     */
    public static > T merge(T t1 , T t2){
        if(null == t1){return t2;}
        if(null == t2){return t1;}

        t1.addAll(t2);
        return t1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy