简体   繁体   English

组合ArrayList没有重复

[英]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);
    }    
}

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. 到目前为止,我的方法将它们加在一起。

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. 另一种方法是将值保存在一个集合(如HashSet )中,不允许任何重复。 Then you can combine them like: 然后你可以将它们组合起来:

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
}   

The simple, no brains solution: 简单,无脑的解决方案:

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

Use Set , it has been created for that purpose. 使用Set ,它是为此目的而创建的。 A Set cannot contain 2 identical elements, based on the equals method. 基于equals方法, Set不能包含2个相同的元素。

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

Using a combination of ArrayList and contains method is an antipattern here. 使用ArrayListcontains方法的组合是一个反模式。

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 ,请参阅其他答案。

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 HashSet object of your ArrayList. 1)通过创建ArrayList的等效HashSet对象,可以获得输出的第一种也是最简单的方法。 Since HashSet does not allow duplicates. 由于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 . 2)还有另一种使用高级for循环的方法 Iterate the second list and check if the current element is not in first list and then add the current element. 迭代第二个列表并检查当前元素是否不在第一个列表中,然后添加当前元素。

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. 注意:第二种方法只有在第一个列表没有重复时才能正常工作。

use contains(Object) method in ArrayList 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);
       }
    }
}

Have you tried ArrayList.addAll() 你试过ArrayList.addAll()吗?

Look at this java doc 看看这个java doc

As pointer out this would not handle duplicates which can easily be removed using a Set 作为指针,这将无法处理可以使用Set轻松删除的重复项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM