检查一个 ArrayList 是否包含来自另一个 ArrayList(或 Collection)的每个元素 [英] Check if an ArrayList contains every element from another ArrayList (or Collection)
本文介绍了检查一个 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屋!
查看全文