Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two arrayLists into a new arrayList, with no duplicates and in order, in Java

Tags:

java

arraylist

I am trying to "combine" two arrayLists, producing a new arrayList that contains all the numbers in the two combined arrayLists, but without any duplicate elements and they should be in order. I came up with this code below. I run through it and it makes sense to me, but Im not sure if I can be using < or > to compare get(i)'s in arrayLists. I am adding all the elements in array1 into the plusArray. Then I am going through the plusArray and comparing it to array2 to see if any of array2's elements exist inside plusArray. If they do I am doing nothing, but if they dont then I am trying to add it in its correct position. Perhaps my nested for loops being used incorrectly? Note: The ArrayLists are presorted by the user in increasing order.

     ArrayList<Integer> plusArray = new ArrayList<Integer>(); for(int i = 0; i < array1.size(); i++){     plusArray.add(array1.get(i)); }  for(int i = 0; i < plusArray.size(); i++){     for(int j = 0; j < array2.size(); j++){      if(array2.get(j) < plusArray.get(i)){         plusArray.add(i,array2.get(j));     }     else if(plusArray.get(i).equals(array2.get(j))){         ;     }     else if(array2.get(j) > plusArray.get(i)){         plusArray.add(i, array2.get(j));     }  } 

UPDATE: I dont get the exception below anymore. Instead it seems the program runs forever. I changed the location of where to add the elements in the < and > conditions. /// Here is the exception that I get when my array lists are: IntSet 1: { 1 2 } IntSet 2: { 1 3 4 }

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.util.Arrays.copyOf(Unknown Source) at java.util.ArrayList.grow(Unknown Source) at java.util.ArrayList.ensureCapacityInternal(Unknown Source) at java.util.ArrayList.add(Unknown Source) at IntSet.plus(IntSet.java:92) at IntSetDriver.main(IntSetDriver.java:61) 
like image 890
Milwaukoholic Avatar asked Mar 29 '12 00:03

Milwaukoholic


People also ask

How do you combine ArrayLists in Java?

Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.

How do I merge two sorted ArrayLists?

Step 1 : Let arrayA and arrayB be two sorted integer input arrays. Step 2 : Declare mergedArray with combined size of arrayA and arrayB . Step 4 : Smaller element in arrayA[i] and arrayB[j] is assigned to mergedArray[k] .

How do you add an element to an ArrayList to another?

The addAll() method is used to add all the elements from one ArrayList to another ArrayList.


2 Answers

Firstly remove duplicates:

arrayList1.removeAll(arrayList2); 

Then merge two arrayList:

arrayList1.addAll(arrayList2); 

Lastly, sort your arrayList if you wish:

collections.sort(arrayList1); 

In case you don't want to make any changes on the existing list, first create their backup lists:

arrayList1Backup = new ArrayList(arrayList1); 
like image 104
Ediz Türkoğlu Avatar answered Oct 06 '22 00:10

Ediz Türkoğlu


Instead of the code you wrote, you may use ArrayList.addAll() to merge the lists, Collections.sort() to sort it and finally traverse of the resulting ArrayList to remove duplicates. The aggregate complexity is thus O(n)+O(n*log(n))+O(n) which is equivalent to O(n*log(n)).

like image 45
iehrlich Avatar answered Oct 05 '22 22:10

iehrlich