[英]Combining ArrayList without duplicates
import java.util.ArrayList;
import java.util.Collections;
public class SmartCombining {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list1, 4, 3);
Collections.addAll(list2, 5, 10, 4, 3, 7);
smartCombine(list1, list2);
System.out.println(list1);
System.out.println(list2);
}
public static void smartCombine(ArrayList<Integer> first,
ArrayList<Integer> second) {
first.addAll(second);
}
}
所以,我想将两个列表合并为一个,但如果第二个列表包含第一个列表,则不会添加。 到目前为止,我的方法将它们加在一起。
好吧,一种方法是迭代第二个列表,同时检查第一个列表中是否存在每个元素。 如果没有,请添加它。
public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) {
for(Integer num : second) { // iterate through the second list
if(!first.contains(num)) { // if first list doesn't contain current element
first.add(num); // add it to the first list
}
}
}
另一种方法是将值保存在一个集合(如HashSet
)中,不允许任何重复。 然后你可以将它们组合起来:
first.addAll(second);
您可以做的另一种方法是首先从第二个列表中存在的第一个列表中删除所有元素(那些将被复制的元素)。 然后,将第二个列表的所有元素添加到第一个列表中。
public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) {
first.removeAll(second); // remove elements that would be duplicated
first.addAll(second); // add elements from second list
}
简单,无脑的解决方案:
Set<Integer> joinedSet = new HashSet<Integer>();
joinedSet.addAll(list1);
joinedSet.addAll(list2);
使用Set
,它是为此目的而创建的。 基于equals
方法, Set
不能包含2个相同的元素。
Set<Integer> list1 = new HashSet<Integer>();
Set<Integer> list2 = new HashSet<Integer>();
使用ArrayList
和contains
方法的组合是一个反模式。
删除重复项,然后合并两个列表:
list1.remove(list2);
list1.addAll(list2);
如果您不想更改原始列表,请先创建备份:
list1BP = new ArrayList(list1);
另一种方法是使用HashSet ,请参阅其他答案。
有两种简单的方法可以组合两个列表,并且将删除重复。
1)通过创建ArrayList的等效HashSet对象,可以获得输出的第一种也是最简单的方法。 由于HashSet不允许重复。
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list1, 4, 3);
Collections.addAll(list2, 5, 10, 4, 3, 7);
System.out.println(smartCombine(list1, list2));
}
public static HashSet<Integer> smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) {
first.addAll(second);
HashSet<Integer> hs = new HashSet<Integer>(first);
return hs;
2)还有另一种使用高级for循环的方法 。 迭代第二个列表并检查当前元素是否不在第一个列表中,然后添加当前元素。
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list1, 4, 3);
Collections.addAll(list2, 5, 10, 4, 3, 7);
smartCombine(list1, list2);
System.out.println(list1);
}
public static void smartCombine(ArrayList<Integer> first, ArrayList<Integer> second) {
for (Integer num : second) {
if (!first.contains(num)) {
first.add(num);
}
}
}
注意:第二种方法只有在第一个列表没有重复时才能正常工作。
在ArrayList
使用contains(Object)
方法
public static void smartCombine(ArrayList<Integer> first,
ArrayList<Integer> second) {
for(Integer i :second){
if(!first.contains(i)) { // if first list doesn't contain this item, add item to the first list.
first.add(i);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.