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 is adding them all together.

+3


source to share


7 replies


Well, one way to do this is to loop over the second list, checking if every item exists in the first list. If not, 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 is to keep your values ​​inside a set (for example HashSet

), which doesn't allow duplicates. Then you can combine them like this:



first.addAll(second);

      

Another way to do this is to first remove all items from the first list that exist in the second list (those that will be duplicated). Then you add all the 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
}   

      

+2


source


Simple solution without brains:



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

      

+3


source


Use it Set

, it was created for this purpose. A Set

cannot contain 2 identical elements based on a method equals

.

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

      

Using a combination of techniques ArrayList

, and contains

here is antipattern.

+2


source


Remove duplicates, then merge both lists:

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

      

If you don't want to change the original list, first create a backup:

list1BP = new ArrayList(list1);

      

Another approach is to use HashSet , see other answers.

+1


source


use the contains(Object)

method inArrayList

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

      

0


source


You tried ArrayList.addAll()

Look at the java doc

As a pointer, this will not handle duplicates, which can be easily removed with Set

0


source


There are two easy ways to combine two lists and the duplicate will be removed.

1) The first and easiest way to get your result is by creating an equivalent HashSet object of your ArrayList. Since HashSet doesn't allow duplicate.

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 to use extended for loop . Iterate through the second list and check if the current item is not in the first list, then add the current item.

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 method will work fine only if the first list has no duplicates.

0


source







All Articles