(原创)判断List列表中是否存在重复元素

今天碰到一个实际问题

如何判断一个List列表中的元素是否有重复的

想到不可能一个个去对比

于是去查了下,发现可以利用HashSet的特性去做

因为Set集合中是不允许存在重复元素的

于是得到如下处理方式

    /**
     * 列表中是否包含重复元素
     *
     * @param list
     * @return
     */
    public static boolean isRepeatList(List list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        Set set = new HashSet<>(list);
        if (set.size() == list.size()) {
            return false;
        } else {
            return true;
        }
    }

猜你喜欢

转载自blog.csdn.net/Android_xiong_st/article/details/108646542