permaculture playing cards*
The moose likes Beginning Java and the fly likes How to remove common item between 2 ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

How to remove common item between 2 ArrayList

rushikesh kale
Ranch Hand

Joined: Jan 25, 2005
Posts: 41

Hi Friends,

I created 2 ArrayList as follows:


Now i want modify List A1 such that, it will contain only element which are not present in List B1.

How can I do this?
Eugene Wee
Greenhorn

Joined: Dec 17, 2007
Posts: 5
If B2 may have many elements, then it may be best to sort B2, and then iterate over B1, using binary search to find an element in B2 that is equal to the current element in B1. If such an element is found, remove the current element in B1.

On the other hand, if B1 may have many elements but B2 has only a few elements, then you could do the above, but sort B1 instead and iterate over B2.

If both B1 and B2 will have only a few elements, then you could iterate over B2 and use B1's remove() method that takes an Object with the current element in B2.
[ May 09, 2008: Message edited by: Eugene Wee ]
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Check "removeAll(Collection<?> c)" method in collections framework.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
You might want to give a try to java.util.List's method removeAll(). Don't know how this method is implermented in ArrayList, so maybe one of the above approaches is faster, but using the JCF's on-board methods is usually a good idea.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

The java.util.ArrayList is derived from java.util.AbstractList which again is derived from java.util.AbstractCollection. Now in the AbstractCollection class there are two functions namely,
removeAllInternal(Collection c) and
retainAllInternal(Collection c)
but both of them are declared private so ArrayList cannot publicly override them.

You should be better off calling remove(Object obj) with each element in the collection containing objects to be removed. This method removes the first instance of obj so if you want to remove all occurrences you will have to take care of that. If the object has been found and removed successfully it returns a true otherwise a false is returned.

Hope this is what you were looking for?

Cheers,
Raj.
Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
@
just iterate over both array list and test if both the list has same element,then call remove method on that index..
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171

Results:

1
9


Have Fun!


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
this is the code i wrote but it throws run time exception,can any one tell why?




Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Because you are iterating and removing elements at the same time (ConcurrentModificationException)
Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
@
so how can i remove tht error for accomplishing the goal
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Why are you not trying what Purushothaman Thambu, Guido Sautter and Raj Kamal are proposing above?

With removeAll(...) you can do it in one line of code...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Well Jesper,

I was quickly going through the javadocs and messaged the first one that caught my eye. Sorry if I mislead the original poster.....

Cheers,
Raj.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Originally posted by fundupuneet vyas:
@
so how can i remove tht error for accomplishing the goal

Remove using the iterator; call l1.remove() instead of A1.remove(o);

And that basically removes the need for your code altogether and just replace it with A1.removeAll(B1), since this is the code of Java 6's implementation of AbstractCollection.removeAll (which is inherited by ArrayList):


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: How to remove common item between 2 ArrayList