Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to remove common item between 2 ArrayList

 
Ranch Hand
Posts: 41
Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Check "removeAll(Collection<?> c)" method in collections framework.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 454
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Results:

1
9


Have Fun!
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Because you are iterating and removing elements at the same time (ConcurrentModificationException)
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
@
so how can i remove tht error for accomplishing the goal
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 454
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic