使用java.util.List的containsAll()方法可能导致的问题

今天在偶然之间发现了一个bug,原因居然是使用了containsAll()方法,这个问题很简单,看以下代码就能发现很大的问题。

java 复制代码
package collection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
public class ListExample {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        list.add(2);
        list.add(3);
        list.add(3);

        List<Integer> integerList = new ArrayList<>();

        integerList.add(3);
        integerList.add(3);
        integerList.add(3);

        System.out.println(list);
        System.out.println(integerList);
        System.out.println(list.containsAll(integerList));
    }

}

上面的结果最后一行打印的是true,因为containsAll()方法的作用类似于遍历指定的集合c,通过contains()比较集合中每个元素,如果有元素不包含在当前的list对象中,就返回false,否则返回true,为了便于理解,写了以下伪代码

java 复制代码
public interface List<E> extends Collection<E> {

    public boolean containsAll(Collection<?> c) {
        for (Object o : c) {
            if (!this.contains()) {
                return false;
            }
        }
        
        return true;
    }

}

因此,文章给出的代码等价于

java 复制代码
package collection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
public class ListExample {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();

        list.add(2);
        list.add(3);
        list.add(3);

        System.out.println(list);
        System.out.println(integerList);
        System.out.println(list.contains(3));
    }

}

总结:这篇文章分享了一下项目中遇到的关于containsAll()方法使用时应该考虑到的问题,当比较的两个list中元素个数相同时,可以用equals()方法替代containsAll()方法使用,但是在使用之前需要对两个集合排序(因为List的源码里已经说明了,只有但集合元素的个数和顺序都一样才返回true)。

复制代码
/**
 * Compares the specified object with this list for equality.  Returns
 * true if and only if the specified object is also a list, both
 * lists have the same size, and all corresponding pairs of elements in
 * the two lists are equal.  (Two elements e1 and
 * e2 are equal if (e1==null ? e2==null :
 * e1.equals(e2)).)  In other words, two lists are defined to be
 * equal if they contain the same elements in the same order.  This
 * definition ensures that the equals method works properly across
 * different implementations of the List interface.
 *
 * @param o the object to be compared for equality with this list
 * @return true if the specified object is equal to this list
 */
boolean equals(Object o);

好了,文章就分享到这里了,感谢阅读~

相关推荐
熙客4 分钟前
Java集合框架概述
java·开发语言
一只会写代码的猫12 分钟前
深度解析 Java、C# 和 C++ 的内存管理机制:自动 vs 手动
java·jvm·算法
我命由我1234515 分钟前
Java 开发 - 简单消息队列实现、主题消息队列实现
java·开发语言·后端·算法·java-ee·消息队列·intellij-idea
chilavert31815 分钟前
技术演进中的开发沉思-194 JavaScript: Prototype 框架
开发语言·javascript·原型模式
2501_9412374515 分钟前
高性能计算通信库
开发语言·c++·算法
杜子不疼.22 分钟前
【C++】红黑树为什么比AVL快?用C++亲手实现告诉你答案
开发语言·c++
程序猿追24 分钟前
Ascend C编程范式总结:与CUDA的异同对比
c语言·开发语言·算法
float_六七26 分钟前
SQL中=与IS的区别:关键用法解析
java·数据库·sql
rit843249927 分钟前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
沐知全栈开发1 小时前
HTML DOM 修改
开发语言