检查ArrayList是否包含另一个ArrayList(或Collection)中的每个元素 [英] Check if an ArrayList contains every element from another ArrayList (or Collection)

查看:111
本文介绍了检查ArrayList是否包含另一个ArrayList(或Collection)中的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能在这里找不到一个简单的单层纸,但这是我的问题:

There is probably a simple one-liner that I am just not finding here, but this is my question:

如何检查ArrayList是否包含另一个ArrayList中的所有对象?我正在寻找(如果存在的话)类似的东西:

How do I check if an ArrayList contains all of the objects in another ArrayList? I am looking (if it exists) for something along the lines of:

//INCORRECT EXAMPLE:
if(one.contains(two))
{
    return true;
}
else
{
    return false;
}

例如:

ArrayList one = {1, 2, 3, 4, 5}

ArrayList two = {1, 2, 3} --> True
ArrayList two = {} --> True
ArrayList two = {1, 2, 3, 4, 5} --> True
ArrayList two = {1, 5, 2} --> True
ArrayList two = {1, 7, 4} --> False
ArrayList two = {0, 1, 3} --> False
ArrayList two = {4, 5, 6} --> False
ArrayList two = {7, 8, 9} --> False

推荐答案

java.util.Collection接口中声明了一种称为containsAll的方法.在您的设置中,one.containsAll(two)会给出所需的答案.

There is a method called containsAll declared in the java.util.Collection interface. In your setting one.containsAll(two) gives the desired answer.

这篇关于检查ArrayList是否包含另一个ArrayList(或Collection)中的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆