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

com.github.bingoohuang.utils.lang.Collects Maven / Gradle / Ivy

package com.github.bingoohuang.utils.lang;

import lombok.val;

import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

public class Collects {
    /**
     * 测试一个集合中,是否包含任一元素。
     *
     * @param collection 集合
     * @param values     测试包含元素
     * @param         集合元素类型
     * @return 是否包含
     */
    public static  boolean containsAnyOf(Collection collection, T... values) {
        for (T t : values) {
            if (collection.contains(t)) return true;
        }
        return false;
    }



    /**
     * 在一个集合中查找指定属性,是否与targetValue相等
     *
     * @param collection  集合
     * @param targetValue 目标值
     * @param getter      集合元素属性对应的getter
     * @param          集合元素类型
     * @param          目标值类型
     * @return true 在集合中找到了与指定属性相等
     */
    public static  boolean exists(Collection collection, V targetValue, Function getter) {
        val foundItem = find(collection, targetValue, getter);
        return foundItem.isPresent();
    }

    /**
     * 在一个集合中查找指定属性,是否与targetValue相等
     *
     * @param collection  集合
     * @param targetValue 目标值
     * @param getter      集合元素属性对应的getter
     * @param          集合元素类型
     * @param          目标值类型
     * @return true 在集合中找到了与指定属性相等
     */
    public static  Optional find(Collection collection, V targetValue, Function getter) {
        for (T item : collection) {
            if (Objects.equals(targetValue, getter.apply(item))) {
                return Optional.of(item);
            }
        }

        return Optional.empty();
    }

    public static boolean isEmpty(Collection list) {
        return list == null || list.isEmpty();
    }

    public static boolean isNotEmpty(Collection list) {
        return list != null && !list.isEmpty();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy