[英]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.