java 查找两个 ArrayList 之间的交集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26222864/
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
Find intersection between two ArrayLists
提问by user3366504
Find intersection of two ArrayLists of Strings.
查找字符串的两个 ArrayLists 的交集。
Here is the code:
这是代码:
public ArrayList<String> intersection( ArrayList<String> AL1, ArrayList<String> AL2){
ArrayList<String> empty = new ArrayList<String>();
ArrayList<String> empty1 = new ArrayList<String>();
if (AL1.isEmpty()){
return AL1;
}
else{
String s = AL1.get(0);
if(AL2.contains(s))
empty.add(s);
empty1.addAll(AL1.subList(1, AL1.size()));
empty.addAll(intersection(empty1, AL2));
return empty;
}
}
I want the output to look like this: For example,
我希望输出看起来像这样:例如,
[a, b, c] intersect [b, c, d, e] = [b, c]
The above code give me this output, but I want to know how to make this code more easier to understand.
上面的代码给了我这个输出,但我想知道如何使这段代码更容易理解。
回答by John Bollinger
You could make it easier to understand by writing it like this:
你可以这样写,让它更容易理解:
/**
* Computes the intersection of two Lists of Strings, returning it as a new ArrayList of Strings
*
* @param list1 one of the Lists from which to compute an intersection
* @param list2 one of the Lists from which to compute an intersection
*
* @return a new ArrayList of Strings containing the intersection of list1 and list2
*/
public ArrayList<String> intersection( List<String> list1, List<String> list2) {
ArrayList<String> result = new ArrayList<String>(list1);
result.retainAll(list2);
return result;
}
回答by mistahenry
Java collections already have support for this with the retainAll
call. Rather than return a new set, the intersection happens in place, which is why you must create a new ArrayList if you want to retain the original list1. retainAll
returns a boolean if the calling object is modified
Java 集合已经通过retainAll
调用对此提供了支持。交集不是返回新集合,而是原地发生,这就是为什么如果要保留原始列表 1 就必须创建新的 ArrayList。retainAll
如果调用对象被修改,则返回一个布尔值
ArrayList<String> list1 = new ArrayList<String>();
list1.add("A");
list1.add("B");
list1.add("C");
ArrayList<String> list2 = new ArrayList<String>();
list2.add("D");
list2.add("B");
list2.add("C");
ArrayList<String> intersection = new ArrayList<String>(list1);
intersection.retainAll(list2);
for(String s: intersection){
System.out.println(s);
}
Output:
输出:
B
C
回答by Waldemar Schneider
If you are ok with a dependency, i would recommend you to take a look at apache commons collections (http://commons.apache.org/proper/commons-collections/release_4_0.html).
如果您对依赖项没问题,我建议您查看 apache 公共集合(http://commons.apache.org/proper/commons-collections/release_4_0.html)。
For your specific use it would be the method intersection from CollectionUtils (https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html)
对于您的特定用途,它将是 CollectionUtils 的方法交集(https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html)
回答by moonlighter
You can use Apache Commons Collections ListUtils.intersection
您可以使用 Apache Commons Collections ListUtils.intersection
http://commons.apache.org/proper/commons-collections/javadocs/api-2.1.1/org/apache/commons/collections/ListUtils.html#intersection(java.util.List, java.util.List)
http://commons.apache.org/proper/commons-collections/javadocs/api-2.1.1/org/apache/commons/collections/ListUtils.html#intersection(java.util.List, java.util.List)
回答by brso05
public ArrayList<String> intersection( ArrayList<String> AL1, ArrayList<String> AL2){
ArrayList<String> returnArrayList = new ArrayList<String>();
for(String test : AL1)
{
if(!returnArrayList.contains(test))
{
if(AL2.contains(test))
{
returnArrayList.add(test);
}
}
}
return returnArrayList;
}
You could use loops instead of recursion.
您可以使用循环而不是递归。