判断集合中存在String字符串 或 判断集合中不存在String字符串

 

一.使用场景

用于集合中有多个相近的字符,无法使用包含判断

如:

 

 

 这里如果我想判断以上集合中是否包含“信封件-DE”就会被“信封件-DE2”影响到

毕竟:“信封件-DE2”包含“信封件-DE”

二."判断集合中存在String字符串"代码

    /**
     * 判断集合中存在String字符串
     *
     * @param collection
     * @param str
     * @return
     */
    public static boolean isExist(Collection collection, String str)
    {
        if (collection != null && StringUtils.isNotEmpty(str))
        {
            Iterator it = collection.iterator();

            while(it.hasNext())
            {
                if (str.equals(it.next()))
                {
                    return true;
                }
            }
        }

        return false;
    }

三.“判断集合中不存在String字符串”代码

    /**
     * 判断集合中不存在String字符串
     *
     * @param collection
     * @param str
     * @return
     */
    public static boolean isNotExist(Collection collection, String str) {
        return !isExist(collection, str);
    }

 

posted @ 2022-03-30 16:05  骚哥  阅读(410)  评论(0编辑  收藏  举报