• 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 a item from Arraylist without looping it

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a Array-list which contain list of names.I need to remove one name.I can do this using for loop and find the index of element and delete it.But i need to delete it without looping.Is there any method to delete element by name? or can i use any other Data Structure in Java for this issue?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to loop. There's already an API http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove%28java.lang.Object%29
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jaikiran.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sam liyanage wrote:But i need to delete it without looping.



I don't know where that requirement came from; let me point out that the ArrayList.remove(Object) very likely uses looping to achieve its goal. Is it just your code which isn't allowed to use loops for some reason, or are you opposed to anybody's code using loops?
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a ArrayList which contain many String values.I need to remove one item from it.So i have to lop through the list to find the index of item.Problem is list contain many values it takes much time.So in my app i have to do this process several times.Then it takes much time.can anyone suggest a new method to do this?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So i have to lop through the list to find the index of item.


Maybe not - Jaikiran mentioned how it can be done without looping, provided you have a reference to that object. How have you tried to make that work for you?
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is ArrayList.remove method.but Paul mentioned ArrayList.remove also use loop to remove item.so does that make big different?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A loop is the only way to iterate through a List. I don't see how it makes much of a difference whether your code does the iterating, or the class library code (except that the class library code is probably more efficient).

But all this sounds like premature optimization to me. Are these putative concerns, or have you actually measured it and thus ascertained that there is a performance issue?
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Ulf said++.

Also, if remove in O(N) time is really a concern, then you are probably using a wrong data structure to hold your objects. Consider those implementations that give you an O(1) or close to that.

If you're using an ArrayList, there is going to be looping ( iterating) involved in removing an element from the ArrayList. Even if you know the index of the element, the ArrayCopy part is going to be as good as looping.

Even if you use a secondary structure that allows you to set a deleted flag or something for objects stored in the index positions of the original ArrayList and you don't really remove the object from the original ArrayList but just set the deleted flag in the secondary structure, it will likely give you other things to worry about.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose one way to do it without looping THROUGH THE LIST would be to generate random numbers. Use those to look at the elements in your list to see if it is one you want to remove.

Now this does have a few problems...

1) You need a loop to generate the random numbers
2) It could take a long time before you found the element you want to remove
3) You can never really be SURE your element isn't in the list. You may just be unlucky and not found it.

However, it does satisfy the requirement of not looping through the list.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic