简体   繁体   English

如何检查Arraylist中的两个String值是否包含在另一个Arraylist中?

[英]How to check if two String values from an Arraylist are contained in another Arraylist?

I have an Arraylist that contains two String values, and I want to check if these values are both present in the second Arraylist. 我有一个包含两个String值的Arraylist,我想检查这些值是否存在于第二个Arraylist中。 For some reason, I am stuck, and I can't get my code to work. 出于某种原因,我陷入困境,我无法让我的代码工作。

Here is my code: 这是我的代码:

    public static void main(String[] args) {

    ArrayList <String> list1 = new ArrayList <String>();

    list1.add("bob");
    list1.add("karen");

    ArrayList <String> list2 = new ArrayList <String>();

    list2.add("java");
    list2.add("karen");     
    list2.add("cook");
    list2.add("tablet");
    list2.add("cup");
    list2.add("coffee");

    for (String list11 : list1) {

        for (String list22 : list2) {

            if (list22.contains(list11)){

                System.out.println ("List 2 contains the strings from list1");
            }
        }
    }
}

My if statement is being executed even though all the values from list11 do not exist in list22, but only one of them. 我的if语句正在执行,即使list11中的所有值都不存在于list22中,但只有其中一个。 My goal is to execute the if statement only when all the values in list1 are present in list2, without hardcoding anything because list1 is supposed to grow in size eventually. 我的目标是只有当list1中的所有值都出现在list2中时才执行if语句,而不需要硬编码任何东西,因为list1最终应该增大。

What am I doing wrong? 我究竟做错了什么?

Use this: 用这个:

if(list2.containsAll(list1))
    System.out.println ("List 2 contains the strings from list1");

.containsAll() returns true if all objects in the specified collection (list1) are elements of this List (list2) , & false otherwise. 如果指定集合(list1)中的所有对象都是此List (list2)的元素,则.containsAll()将返回true否则返回false

As in your case list2 is superset of list1 which you are checking, the for loop should look like: 在你的情况下, list2是你正在检查的list1超集,for循环应该如下所示:

boolean allElementsPresent = true;
    for (String list11 : list1) {
            if (!list22.contains(list11)){
                allElementsPresent = false;                
            }
    }
if(allElementsPresent){
    System.out.println ("List 2 contains the strings from list1");
}

There is no need to iterate over list2 objects. 无需迭代list2对象。 You only need to iterate over list1 to check if each and every element exists in list2 or not. 您只需要遍历list1以检查list2是否存在每个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查ArrayList的元素是否都包含在另一个ArrayList中 - How to check whether the elements of an ArrayList are all contained in another ArrayList 打印 ArrayList 的值<ArrayList<Integer> &gt; 不包含在另一个 ArrayList 中的<ArrayList<~> &gt; - Print values of ArrayList<ArrayList<Integer>> that are not contained in another ArrayList<ArrayList<~>> 如何遍历 ArrayList 并检查它是否包含来自 Java 中另一个 ArrayList 的值 - How to loop through an ArrayList and check if it contains values from another ArrayList in Java 检查ArrayList中包含的对象中是否存在某个字符串,然后将该对象添加到另一个ArrayList中 - Checking if there is a certain string in an object contained in an ArrayList, then adding the object to another ArrayList 如何从 ArrayList 存储字符串值<String>到一个 ArrayList<Object> 对象 - How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects 如何从ArrayList获取值 <Hashtable<String, ArrayList<String> &gt;&gt; - how to get values from an ArrayList<Hashtable<String, ArrayList<String>>> 如何从 LinkedHashMap 的值进行组合<string, arraylist<arraylist> ></string,> - How to make combination from values of of LinkedHashMap<String, ArrayList<ArrayList>> 字符串和另一个数组列表的 ArrayList - ArrayList of string and another arraylist 从另一个ArrayList内部的ArrayList访问值 - Access Values from an ArrayList That is inside another ArrayList 从另一个 arrayList 替换 arrayList 值 - Replace arrayList values from another arrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM