使用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);

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

相关推荐
明月看潮生13 分钟前
青少年编程与数学 02-019 Rust 编程基础 08课题、字面量、运算符和表达式
开发语言·青少年编程·rust·编程与数学
什码情况44 分钟前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
天天打码1 小时前
Rspack:字节跳动自研 Web 构建工具-基于 Rust打造高性能前端工具链
开发语言·前端·javascript·rust·开源
Petrichorzncu1 小时前
Lua再学习
开发语言·学习·lua
AA-代码批发V哥1 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
字节高级特工1 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
晴天下小雨o1 小时前
排序算法总结
java·算法·排序算法
曼岛_1 小时前
[Java实战]Spring Boot 整合 Redis(十八)
java·spring boot·redis
向哆哆1 小时前
Netty在Java网络编程中的应用:实现高性能的异步通信
java·网络·php
炯哈哈1 小时前
【上位机——MFC】序列化机制
开发语言·c++·mfc·上位机