This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

How to remove common item between 2 ArrayList

 
rushikesh kale
Ranch Hand
Posts: 41
Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Check "removeAll(Collection<?> c)" method in collections framework.
 
Guido Sautter
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
@
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
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator

Results:

1
9


Have Fun!
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
this is the code i wrote but it throws run time exception,can any one tell why?




 
Manuel Leiria
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Because you are iterating and removing elements at the same time (ConcurrentModificationException)
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
@
so how can i remove tht error for accomplishing the goal
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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...
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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):
 
    Bookmark Topic Watch Topic
  • New Topic