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

3
  • Just call it extend(). That is what Python calls it. :-p Commented Oct 28, 2014 at 18:59
  • If you don't want duplicate values use a set, e.g. a TreeSet<Integer> Commented Oct 28, 2014 at 19:01
  • Don't use a List. This is what a Set is for. Commented Oct 28, 2014 at 19:03

7 Answers 7

7

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:

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
}   
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think OP wants to modify the second list, only the first. Flip that logic around.
@Mr.Polywhirl yes, you are right, minor change, just did it
4

The simple, no brains solution:

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

Comments

3

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.

1 Comment

Looks like people agree with you: List intersection in java.
2

Use Set, it has been created for that purpose. A Set cannot contain 2 identical elements, based on the equals method.

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

Using a combination of ArrayList and contains method is an antipattern here.

1 Comment

The whole point of using lists is to have ORDERED collections. If you switch to HashSet, you loose that. Maybe your answer would be better with LinkedHashSets or something.
1

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. Since HashSet does not allow duplicates.

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.

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.

Comments

0

Have you tried ArrayList.addAll()

Look at this java doc

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

2 Comments

Does not handle duplicates.
i agree it does not. Logic for that will have to be written separately
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.