列出java中的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4349369/
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
List difference in java
提问by learn_plsql
I have two ArrayList<Integer>
as follows:
我有两个ArrayList<Integer>
如下:
original: 12, 16, 17, 19, 101
原来的: 12, 16, 17, 19, 101
selected: 16, 19, 107, 108, 109
选择: 16, 19, 107, 108, 109
I want to do difference on these lists such that in the end I have two lists:
我想在这些列表上做一些不同的事情,这样最终我有两个列表:
Add: 108,109,107
添加: 108,109,107
remove: 12, 17, 101
消除: 12, 17, 101
Length of original and selected lists varies and one can be greater/smaller than the other
原始列表和选定列表的长度各不相同,一个可以大于/小于另一个
采纳答案by Adam
List<Integer> original = Arrays.asList(12,16,17,19,101);
List<Integer> selected = Arrays.asList(16,19,107,108,109);
ArrayList<Integer> add = new ArrayList<Integer>(selected);
add.removeAll(original);
System.out.println("Add: " + add);
ArrayList<Integer> remove = new ArrayList<Integer>(original);
remove.removeAll(selected);
System.out.println("Remove: " + remove);
Output:
输出:
Add: [107, 108, 109]
Remove: [12, 17, 101]
Uses Collection's removeAll method. See javadocs.
使用 Collection 的 removeAll 方法。请参阅 javadoc。
回答by Mark Peters
List<Integer> original;
List<Integer> selected;
List<Integer> add = new ArrayList<Integer>(selected);
add.removeAll(original);
List<Integer> remove = new ArrayList<Integer>(original);
remove.removeAll(selected);
Be careful with border cases around duplicate elements. Should cardinality be respected? As in, if I had 5, 6
originally and 5, 5, 6
after, should add be 5
? The above works better with Set
s, since they don't have duplicates (plus contains()
lookups are faster since they are indexed by the data they contain).
小心重复元素周围的边框情况。是否应该尊重基数?如在,如果我5, 6
原来和5, 5, 6
之后,应该添加是5
?以上对Set
s效果更好,因为它们没有重复项(加上contains()
查找速度更快,因为它们由它们包含的数据索引)。
回答by barti_ddu
As an alternative, you could use CollectionUtilsfrom Apache commons library. It has static intersection, unionand subtractmethods suitable for your case.
作为替代方案,您可以使用Apache 公共库中的CollectionUtils。它具有适合您的情况的静态交集、联合和减法方法。
回答by Peter Lawrey
For intersection and union operations the natural collection type is a Set rather than a List, its also more efficient to use.
对于交集和联合操作,自然集合类型是 Set 而不是 List,使用起来也更高效。
回答by Sergii Shevchyk
Using Guava library:
使用番石榴库:
List<Integer> listA = Lists.newArrayList(12,16,17,19,101);
List<Integer> listB = Lists.newArrayList(16,19,107,108,109);
Set<Integer> intersection = Sets.intersection(Sets.newHashSet(listA), Sets.newHashSet(listB));
listA.removeAll(intersection);
listB.removeAll(intersection);
回答by ACV
Intersection: original.retainAll(selected)
.
交叉路口:original.retainAll(selected)
。
After that original will contain only elements present in both collections. Returns true if anything changed.
之后,原件将只包含两个集合中都存在的元素。如果有任何更改,则返回 true。
WARNING: This method is very slow for large collections
警告:对于大型集合,此方法非常慢
回答by Valentyn Kolesnikov
There is a new library available underscore-java. It can do difference and intersection for lists and arrays. Live example.
有一个新的库underscore-java。它可以对列表和数组进行求差和求交。活生生的例子。
Code example:
代码示例:
List<Integer> original = Arrays.asList(12, 16, 17, 19, 101);
List<Integer> selected = Arrays.asList(16, 19, 107, 108, 109);
List<Integer> add = U.difference(selected, U.intersection(original, selected));
List<Integer> remove = U.difference(original, selected);
回答by Apoorv Singh
Here is a function to find intersection of various collections (more than 2) -
这是一个查找各种集合(超过 2 个)的交集的函数 -
public static <T, C extends Collection<T>> C findIntersection(C newCollection,
Collection<T>... collections) {
boolean first = true;
for (Collection<T> collection : collections) {
if (first) {
newCollection.addAll(collection);
first = false;
} else {
newCollection.retainAll(collection);
}
}
return newCollection;
}
Usage -
用法 -
public static void main(String[] args) {
List<Integer> l1 = List.of(1, 3, 5, 7, 9, 11, 13);
List<Integer> l2 = List.of(1, 2, 3, 5, 8, 13);
List<Integer> l3 = List.of(2, 3, 5, 7, 11, 13);
Set<Integer> intersection = findIntersection(new HashSet<>(), l1, l2, l3);
System.out.println(intersection);
}
回答by pasindupa
Use this method if you want to get intersection of a list of lists
如果您想获得列表列表的交集,请使用此方法
List<Address> resultsIntersectionSet( List<Set<Address>> inputListOfLists )
{
Set<Address> intersection = new HashSet<>();
if ( !inputListOfLists.isEmpty() )
intersection = inputListOfLists.get( 0 );
for ( Set<Address> filterResultList : inputListOfLists )
{
intersection.retainAll( filterResultList );
}
return new ArrayList<>( intersection );
}
回答by Prince Okpoziakpo
package LAB8Pack;
import java.util.HashSet;
import java.util.Iterator;
import java.lang.StringBuilder;
public class HashSetDemo {
public static void main(String[] args) {
HashSet<String> round = new HashSet<String> ();
HashSet<String> green = new HashSet<String> ();
// Add elements to 'round' and 'green' sets
//----------------------------------------------------
round.add("peas");
green.add("peas");
round.add("watermelon");
green.add("watermelon");
round.add("basketball");
green.add("chameleon");
round.add("chameleon");
green.add("grass");
round.add("eyes");
green.add("book");
// Create 'setUnion' and 'setInter'
// ---------------------------------------------------
HashSet<String> setUnion = new HashSet<String>();
// Use this to find the intersection
HashSet<String> SETINTER = new HashSet<String>();
HashSet<String> setInter1 = new HashSet<String>(round);
HashSet<String> setInter2 = new HashSet<String>(green);
// Add all the elements to one set
setUnion.addAll(round);
setUnion.addAll(green);
SETINTER.addAll(setUnion);
// Create an intersection
setInter1.removeAll(green); // Get unique items in round
setInter2.removeAll(round); // Get unique items in green
SETINTER.removeAll(setInter2); // Remove items that are unique to green
SETINTER.removeAll(setInter1); // Remove items that are unique to round
//----------------------------------------------------
// DISPLAY RESULTS
// ===================================================
System.out.println("Content of set round");
System.out.println("-----------------------");
System.out.println(OutputSet(round));
System.out.println("Content of set green");
System.out.println("-----------------------");
System.out.println(OutputSet(green));
System.out.println("Content of set Union");
System.out.println("-----------------------");
System.out.println(OutputSet(setUnion));
System.out.println("Content of set Intersection");
System.out.println("-----------------------");
System.out.println(OutputSet(SETINTER));
}
// METHODS
// =======================================================
static StringBuilder OutputSet (HashSet<String> args) {
Iterator iterator = args.iterator();
StringBuilder sB = new StringBuilder ();
while (iterator.hasNext()) {
sB.append(iterator.next() + " \n");
}
return sB;
}
}