Monica Shiralkar wrote:we need to store certain elements which we can access later
Stephan van Hulst wrote:doesn't really say much about your use case. Access them how? For what?
Campbell Ritchie wrote: I shall let you tell us what the iteration orders of a list, a (non‑priority) queue and a stack are.
Campbell Ritchie wrote:
Use sets when you want unique elements without linking them to keys.
Is there a difference? Remember priority queues behave differentlyMonica Shiralkar wrote:. . .
List : insertion order.
Queue: FIFO order
CorrectStatck: FILO order
Campbell Ritchie wrote:
Is there a difference? Remember priority queues behave differentlyMonica Shiralkar wrote:. . .
List : insertion order.
Queue: FIFO orderCorrectStatck: FILO order
![]()
Paul Clapham wrote:Yes, I've done that a lot in the past, just throwing things into an ArrayList so that I can get them out later and do something with them.
So I switched to Set -- I needed a particular implementation, of course, and HashSet seemed okay.
.
Paul Clapham wrote:I was reading a list of names from a text file, and later I needed to print out some of the names (those with a particular feature). But the main purpose of the code was to do something else with those names, and only afterwards did I need to print out that subset of names. And the subset rules weren't known until the end of the processing. Therefore I needed to store all of the names and print the subset -- in the same order -- later.
Paul Clapham wrote:Yes, I've done that a lot in the past, just throwing things into an ArrayList so that I can get them out later and do something with them.
Campbell Ritchie wrote:If you go through one of your threads and write down the names of people posting, you will end up with a list on your paper. You don't need any processing, re‑ordering or anything. You simply record them. That is what lists are good at.
Campbell Ritchie wrote:That is what lists are good at.
Monica Shiralkar wrote:I read that arrayList is good at random access.But this is not random access use case.This one is like we are storing all names and then when we use it we will iterate and do something.
Paul Clapham wrote:
It's perfectly reasonable to use a List without ever accessing it randomly and nobody will criticize you for that.
without having to use all of its features
Agree with Paul: if you ask me what sort of use case for a List is, I shall say, “normai”.Monica Shiralkar wrote:. . . I meant without the primary feature (as in this case)
Campbell Ritchie wrote:
The primary function of a List is to store object references, and that is what you are doing.
Monica Shiralkar wrote:I think the logic is that just for simply storing elements to be used later, one requires a List and instead of going for LinkedList, the simpler easy choice is ArrayList.
Paul Clapham wrote:If you want to produce an image of a black hole then you need to optimize your processing of terabytes of data very aggressively, for example. But that's where we're coming from. ArrayList? LinkedList? Who cares, there's more important problems to deal with.
Monica Shiralkar wrote:The bad part is that inspite of ArrayList being the collection most frequently used by me (and other developers), I am yet to write any code where I used arraylist for its primary purpose, i,e ordered and random access.
Paul Clapham wrote:LinkedList supports random access, but it has O(N) performance instead of ArrayList's O(1) performance.
Monica Shiralkar wrote:I am yet to write any code where I used arraylist for its primary purpose, i,e ordered and random access.
Stephan van Hulst wrote:
Paul Clapham wrote:LinkedList supports random access, but it has O(N) performance instead of ArrayList's O(1) performance.
By definition, that's not random access.
I think what we may mean to say here is that Sun decided to allow one to use the indexed List methods on a LinkedList. I have seen various people grousing that they shouldn't have done so for the reason you highlight -- the performance is poor. I do see that contrary to my recollection, it does NOT implement the RandomAccess tag Interface, which helps to remind one of that fact. So I use "random access" in lowercase to indicate I can point to any element or range by index, RandomAccess capitalized in Java to mean that it will be pleasantly fast.
Monica Shiralkar wrote:I am yet to write any code where I used arraylist for its primary purpose, i,e ordered and random access.
I wouldn't say that random access is ArrayList's primary purpose. Its primary purpose is to hold an ordered sequence of elements. The fact that it supports random access makes it useful for some use cases that simply don't occur very often. Most of the time when you need constant lookup time, you are better served by hash map.
Monica Shiralkar wrote:...I am yet to write any code where I used arraylist for its primary purpose, i,e ordered and random access...
salvin francis wrote:
Monica Shiralkar wrote:...I am yet to write any code where I used arraylist for its primary purpose, i,e ordered and random access...
Have you ever used the "get()" method from ArrayList in your code ? If yes, please explain what you think it does.