我有两个ArrayList<Integer>
如下:
原文:12, 16, 17, 19, 101
已选中:16, 19, 107, 108, 109
我想在这些列表上做交集/联合,最后我有两个列表:
添加:108,109,107
删除:12, 17, 101
原始列表和所选列表的长度各不相同,一个可以比另一个更大/更小
答案 0 :(得分:48)
作为替代方案,您可以使用Apache commons库中的CollectionUtils。它具有适合您案例的静态交集,联合和减去方法。
答案 1 :(得分:41)
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);
输出:
Add: [107, 108, 109]
Remove: [12, 17, 101]
使用Collection的removeAll方法。 See javadocs.
答案 2 :(得分:24)
交叉点:original.retainAll(selected)
。
之后,原始文件将仅包含两个集合中的元素。如果有任何改变,则返回true。
警告:此方法对于大型集合来说非常慢
答案 3 :(得分:8)
对于交集和联合操作,自然集合类型是Set而不是List,它的使用效率也更高。
答案 4 :(得分:6)
使用Guava库:
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);
答案 5 :(得分:5)
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);
小心重复元素周围的边框情况。基数应该得到尊重吗?如果我原来5, 6
和5, 5, 6
之后,应该添加5
?以上内容适用于Set
,因为它们没有重复项(加contains()
次查找更快,因为它们被包含的数据编入索引)。
答案 6 :(得分:2)
如果要获取列表列表的交集,请使用此方法
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 );
}
答案 7 :(得分:1)
答案 8 :(得分:1)
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;
}
}
答案 9 :(得分:0)
这里是查找各种集合(超过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;
}
用法-
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);
}
答案 10 :(得分:0)
使用Java 8+流:
List<Integer> result = original.stream()
.distinct()
.filter(selected::contains)
.collect(Collectors.toList());