比较java中两个arrayList的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20176087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:45:01  来源:igfitidea点击:

comparing elements of two arrayList in java

java

提问by SunJCarkeY

i got two String type arraylist ..one list containing “book1”, “book2”, “book3” and “book4”. And another arrayList contains “book1”, “book2”, “book3”. So, size of first list is 4 and second is 3. And I created another arrayList equal to the size of first list

我得到了两个字符串类型的数组列表。一个包含“book1”、“book2”、“book3”和“book4”的列表。另一个数组列表包含“book1”、“book2”、“book3”。所以,第一个列表的大小是 4,第二个是 3。我创建了另一个等于第一个列表大小的 arrayList

List<Integer> comparingList = new ArrayList<Integer>();
                    //adding default values as one
                    for(int a=0;a<firstList.size();a++){
                        comparingList.add(0);

                    }

And if any content is equal between two lists, I'm setting 1 instead of 0.

如果两个列表之间的任何内容相等,我将设置为 1 而不是 0。

so the new arrayList(comparingList) should have 1,1,1,0 elements

所以新的 arrayList(comparingList) 应该有 1,1,1,0 个元素

for(int counter = 0;counter < firstList.size();counter++){
for(int counter1 = 0;counter1 < secondList.size();counter1++){
if(firstList.get(counter).equals(secondList.get(counter1))){
    comparingList.set(counter,1);
    break;
}
}

}

but when i do this, i'm not being able to set 1 as I can't get into if condition, can anyone help me please

但是当我这样做时,我无法设置 1,因为我无法进入 if 条件,任何人都可以帮助我

采纳答案by Santosh Joshi

Only iterate on first arraylist with larger length and check for containsin second arraylist , if found set one else do nothing

仅迭代具有较大长度的第一个数组列表,并检查第二个数组列表中的包含,如果找到设置,则不执行任何操作

for(int counter = 0; counter < firstList.size(); counter++) {
    if(secondList.contains(firstList.get(counter))) {
          comparingList.set(counter,1);
      }
  }


Whole java program

整个java程序

Just try to run the below program in http://www.compileonline.com/compile_java_online.php

只需尝试在http://www.compileonline.com/compile_java_online.php 中运行以下程序

import java.util.ArrayList;
import java.util.List;

public class CompareArrayListTest{

 public static void main(String[] args) {

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

    firstList.add("book1");
    firstList.add("book2");
    firstList.add("book3");
    firstList.add("book4");

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

    secondList.add("book1");
    secondList.add("book2");
    secondList.add("book3");

    List<Integer> comparingList = new ArrayList<Integer>();
    // adding default values as one
    for (int a = 0; a < firstList.size(); a++) {
        comparingList.add(0);

    }

    for (int counter = 0; counter < firstList.size(); counter++) {
        if (secondList.contains(firstList.get(counter))) {
            comparingList.set(counter, 1);
        }
    }

    System.out.println(comparingList);

}


BitSet bitset = new BitSet();
// adding default values as one
for (int a = 0; a < firstList.size(); a++) {
    comparingList.add(0);

}

for (int counter = 0; counter < firstList.size(); counter++) {
    for (int counter2 = 0; counter < secondList.size(); counter++) {
        if (secondList.get(counter2).equals(firstList.get(counter))) {
            bitset.set(counter, 1);
        }
    }
}

回答by Amir Kost

You can transform the lists to sets, and then use Set.retainAll method for intersection between the different sets. Once you intersect all sets, you are left with the common elements, and you can transform the resulting set back to a list.

您可以将列表转换为集合,然后使用 Set.retainAll 方法进行不同集合之间的交集。一旦您将所有集合相交,就剩下公共元素,您可以将结果集合转换回列表。