51

I have two arrayLists

ArrayList one = {A, B, C, D, E}
ArrayList two = {B, D, F, G}  

I want to have my final ArrayList which will have All the elements of one and the elements which are only in two and not in one.

So ArrayList final = {A, B, C, D, E, F, G}.

How can I do this?

5
  • 5
    Is using a Set possible? This requires that your items in list one are unique. Commented May 13, 2013 at 10:39
  • I am not familiar with Set. So i dont prefer using it. And I am having Java 1.2 in my device. Commented May 13, 2013 at 10:46
  • Lack of familiarity is no reason to shun an excellent data type. Set is available in 1.2. The question you must answer is: do you want any duplicates in your final list/set? Commented May 13, 2013 at 10:58
  • @DuncanJones No i dont want any duplicates in my final list/set. Could you point me on how to do this with Set Commented May 13, 2013 at 15:39
  • At least one of the answers already shows you how to do this. Commented May 13, 2013 at 19:17

4 Answers 4

77

Either:

Set<Foo> fooSet = new LinkedHashSet<>(one);
fooSet.addAll(two);
List<Foo> finalFoo = new ArrayList<>(fooSet);

or

List<Foo> twoCopy = new ArrayList<>(two);
twoCopy.removeAll(one);
one.addAll(twoCopy);
Sign up to request clarification or add additional context in comments.

4 Comments

However, the OP is using Java 1.2 so a re-write to remove generics may be better.
For the second solution, why should we create a twoCopy? Can we just one.removeAll(two); one.addAll(two): ?
You need the copy only if you don't want to modify the original two list. Regarding the second question: the order of the elements wouldn't be the same.
@CongWang In case you don't need the original two list. You can two.removeAll(one); one.addAll(two)
57
for (Object x : two){
   if (!one.contains(x))
      one.add(x);
}

assuming you don't want to use the set suggested in the comment. If you are looking for something fancier than this please clarify your question.

6 Comments

For each not supported in java version 1.2 or 1.4. My device is running java 1.2.
so don't use a for each and use a for instead
Ya.. thats what i tried. But i was just wondering if there is any better way or is this the best way..
If you want to stick with a List this is a perfectly acceptable way of doing it. Depending on the size of you data set a Set or SortedList might give you better performance even if you have to then copy back to a List
@JohnB ur solution is O(n^2) right?
|
4

Try this kind of thing. As Set doesn't allow duplicates you can add only the changes

ArrayList<String> a=new ArrayList<>();
a.add("a");
a.add("b");
ArrayList<String> b=new ArrayList<>();
a.add("a");
a.add("c");
Set<String> s=new HashSet<String>();
s.addAll(a);
s.addAll(b);
a=new ArrayList<>(s);
for(String r:a){
    System.out.println(r);
}

1 Comment

Your code snippet is unnecessarily large. Assume we know how to package it into a main and where to find normal imports. Just include the code.
3

you can do something like this:

ArrayList<Object> result = new ArrayList<>();
result.addAll(one);

for(Object e: two){
    if(!result.contains(e))
        result.add(e);
}

3 Comments

You need to initialize result. You could just do ArrayList result = new ArrayList(one); instead of the first loop.
@Keppil well its not the exact code anyway, arraylist is supposed to be generic, i was just giving an idea
@ay89 OP is using Java 1.2, so no generics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.