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?
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?
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?
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?
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?
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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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