简体   繁体   English

检查一个 ArrayList 是否包含另一个 ArrayList 作为元素

[英]Checking if an ArrayList contains another ArrayList as element

I have a list of lists and I would like to add a list to it, without duplicate.我有一个列表列表,我想向其中添加一个列表,不要重复。 In other to do that, I would like to check if that list is already contained in the main list.在其他方面,我想检查该列表是否已包含在主列表中。 I have written something like this我写过这样的东西

import java.util.ArrayList;
public class Test{
public static void main(String [] args)
 {
  ArrayList<ArrayList<String>> Main = new ArrayList<>();
  ArrayList<String> temp = new ArrayList<>();
  temp.add("One");
  temp.add("Two");
  temp.add("Three");
  Main.add(temp);// add this arraylist to the main array list
  ArrayList<String> temp1 = new ArrayList<>();

  temp1.add("One");
  temp1.add("Two");
  temp1.add("Three");

  if(!Main.containsAll(temp1)) // check if temp1 is already in Main
   {
    Main.add(temp1);
   }
 }
}

When I print the contents of Main , I obtain both temp and temp1 .当我打印Main的内容时,我同时获得了temptemp1 How can I fix this?我怎样才能解决这个问题?

You can use the method List#contains() since contains will check for instances of ArrayList that are equal to the provided ArrayList which will be the case here as temp.equals(temp1) returns true since the method equals of an AbstractList compares their content and here the content of those ArrayList is equal.您可以使用List#contains()因为contains将检查与提供的ArrayList相等的ArrayList实例,这里就是这种情况,因为temp.equals(temp1)返回true因为AbstractList的方法equals比较它们的内容在这里,这些ArrayList的内容是相等的。

if(!Main.contains(temp1)) // check if temp1 is already in Main
{
    Main.add(temp1);
}

Since you want to avoid duplicate lists (and not check for the elements of the inner lists), just use Main.contains instead of Main.containsAll .由于您想避免重复列表(而不是检查内部列表的元素),只需使用Main.contains而不是Main.containsAll

This will check whether the Main list already contains a list with the elements you are about to add.这将检查Main列表是否已经包含一个包含您将要添加的元素的列表。

The issue here is that you're getting confused with the use of containsAll and the list of lists.这里的问题是您对containsAll和列表列表的使用感到困惑。

containsAll is a method that checks whether this collection contains all of the elements of the given collection. containsAll是一种检查此集合是否包含给定集合的所有元素的方法。 In this case:在这种情况下:

  • This collection has 1 element, which is a List<String> ;这个集合有 1 个元素,它是一个List<String>
  • The given collection has 3 elements, which are "One , "Two" and "Three" .给定的集合有 3 个元素,分别是"One , "Two""Three"

And clearly, this collection, which only contains a List<String> (which is ["First, "Two", "Three"] ), does not contain the 3 elements; it only contains a list of those three elements.很明显,这个只包含List<String> (即["First, "Two", "Three"] )的集合包含这 3 个元素;它只包含这三个元素的列表。

So what you really want here isn't containsAll , but contains , ie you want to check whether your list contains another list (and not its elements).所以你真正想要的不是containsAll ,而是contains ,即你想检查你的列表是否包含另一个列表(而不是它的元素)。

The following works:以下工作:

if (!Main.contains(temp1)) {
   Main.add(temp1);
}

and will result in Main being [[One, Two, Three]] , added just once.并将导致Main[[One, Two, Three]] ,只添加一次。

The side question is: why does it work?附带的问题是:为什么它有效? Well now, the question is: does my List<List<String>> , which is [[One, Two, Three]] , contains this List<String> , which is [One, Two, Three] ?现在,问题是:我的List<List<String>>是否包含[[One, Two, Three]] ,包含这个List<String> ,即[One, Two, Three] Since two lists are equal when they have the same size and all of their elements are equal, it does contain it.由于两个列表在大小相同并且它们的所有元素都相等时是相等的,因此它确实包含它。

What would you do if it was about ArrayList<Integer> ?如果是关于ArrayList<Integer>你会怎么做? There is such a method named contains() .有一个名为contains()的方法。 To check if your main list contains some object (another list) just invoke this function passing it as the parameter.要检查您的主列表是否包含某个对象(另一个列表),只需调用此函数将其作为参数传递。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM