• Post Reply 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to remove object of perticular class from ArrayList..?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I have Three classes p1,p2,p3 and i had object for that perticular class's

I had arraylist in which it stores all the objects of three different classes and i want to remove the objects of p3 in that arraylist with in one statement .and i had one clue that removeAll() method in collection which takes collection as its parameter.please give me a hand for this problem
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:Dear all,
I have Three classes p1,p2,p3 and i had object for that perticular class's

I had arraylist in which it stores all the objects of three different classes and i want to remove the objects of p3 in that arraylist with in one statement .and i had one clue that removeAll() method in collection which takes collection as its parameter.please give me a hand for this problem



you can use remove method
like


 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but we have to check it through the loop i want to remove at once
 
Ranveer K Kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:but we have to check it through the loop i want to remove at once


use


and if match found break the loop

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and if match found break the loop


Please understand the problem correctly i must not use loop ok
 
Ranveer K Kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:

and if match found break the loop


Please understand the problem correctly i must not use loop ok


Dear Gowda,

My first post was without loop.
Its not matter you are using loop or without loop.

Please not confused.
for example..
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranveer K Kumar

You are removing only one object of perticular class see if you are having so many object of perticular class if i remove one object it should remove all the objects of that class from list
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out ArrayList#removeAll(Collection<?> c)

Also, please read http://faq.javaranch.com/java/ShowSomeEffort
If you had checked the API after being told of the remove method, I am sure you would have found the removeAll yourself.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example let P,Q and S are the classes
p1,p2,p3 are the objects of P
q1,q2,q3 are the objects of Q
s1,s2,s3 are the objects of S

List list = new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(q1);
list.add(q2);
list.add(q3);
list.add(s1);
list.add(s2);
list.add(s3);

please remove 'S' objects in that list with out iterating it dont use getClass() and getInstance() method
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pseudo code:
1) Create a sublist for all 'S' Objects.
2) Use the removeAll method on the main list using this sublist.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sir actually i had gone for interview they asked me to not sublist and they had given me the clue that use removeAll method thats all
 
Ranveer K Kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This case best way to use generic to add the object in the list.
In above example if you are using n no. of objects then how will you remove without iterating. the best way to use is loop..
If you are able to remove then use
list.remove(int)
if you know the best way then let me know...

 
Ranveer K Kumar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is tricky Q and atleast my knowledge removeAll will remove all the particular Collection(object) from list, and sublist not to be used so
collection cannot be passed.
I am agree with Maneesh, but also sublist not to be used, loop is already not to used..
then only way that remove mannually one by one...

But the main thing I am not sure about the other methods to solve this problem...Please let me know
if any other solution availabe..
Thanks
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Or even better answer :

This code is wrong because the next letter after Q is R

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not the answer, is it ? Because singleList doesn't accomplish what you are trying to do.
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic