java 组合 ArrayList 没有重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26616350/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:14:28  来源:igfitidea点击:

Combining ArrayList without duplicates

javaarraylist

提问by UkoM

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);
    }    
}

So, I want to combine two lists into one, but if the second list contains a number from the first it won't be added. So far my method adds them all together.

所以,我想将两个列表合并为一个,但如果第二个列表包含第一个列表中的数字,则不会添加。到目前为止,我的方法将它们全部加在一起。

采纳答案by nem035

Well, one way to do it is to iterate through the second list while checking if each element exists in the first list. If it doesn't, add it.

好吧,一种方法是遍历第二个列表,同时检查每个元素是否存在于第一个列表中。如果没有,请添加它。

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
         }
     }
}  

Another way would be for you to hold your values inside a set (like HashSet) which doesn't allow any duplicates. Then you can combine them like:

另一种方法是将您的值保存在一个HashSet不允许任何重复的集合(如)中。然后你可以像这样组合它们:

first.addAll(second);

One more way you could do it is to first remove all elements from the first list that exist in the second list (the ones that would be duplicated). Then you add all elements of the second list to the first list.

另一种方法是首先从第二个列表中存在的第一个列表中删除所有元素(将被复制的元素)。然后将第二个列表的所有元素添加到第一个列表中。

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
}   

回答by Gil Tzadikevitch

The simple, no brains solution:

简单,无脑的解决方案:

Set<Integer> joinedSet = new HashSet<Integer>();
joinedSet.addAll(list1);
joinedSet.addAll(list2);

回答by ToYonos

Use Set, it has been created for that purpose. A Setcannot contain 2 identical elements, based on the equalsmethod.

使用Set,它就是为此目的而创建的。Set根据equals方法,A不能包含 2 个相同的元素。

Set<Integer> list1 = new HashSet<Integer>();
Set<Integer> list2 = new HashSet<Integer>();

Using a combination of ArrayListand containsmethod is an antipattern here.

在这里使用ArrayListcontains方法的组合是一种反模式。

回答by ToYonos

There are two easy way you can combine two Lists and duplicate will be removed.

有两种简单的方法可以组合两个列表并删除重复项。

1) First and very easiest way you can get your output, by creating equivalent HashSetobject of your ArrayList. Since HashSet does not allow duplicates.

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) There is another way using advanced for loop. Iterate the second list and check if the current element is not in first list and then add the current element.

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);
        }
    }
}

Note: The second way will work fine only if first list has no duplicates.

注意:只有当第一个列表没有重复项时,第二种方法才能正常工作。

回答by JosEduSol

Remove duplicates, then merge both lists:

删除重复项,然后合并两个列表:

list1.remove(list2);
list1.addAll(list2);

If you dont want to alter the original list, then first create a backup:

如果您不想更改原始列表,请先创建一个备份:

list1BP = new ArrayList(list1);

Another approach is to use HashSet, see other answers.

另一种方法是使用HashSet,请参阅其他答案。

回答by Chiseled

Have you tried ArrayList.addAll()

你有没有尝试过 ArrayList.addAll()

Look at this java doc

看看这个java文档

As pointer out this would not handle duplicates which can easily be removed using a Set

作为指针,这不会处理可以使用 Set 轻松删除的重复项

回答by Salih Erikci

use contains(Object)method in ArrayList

使用contains(Object)方法ArrayList

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);
       }
    }
}