Originally posted by Ernest Friedman-Hill:
Furthermore, remove() calls get(), so you actually need to use Iterator's delete method to delete items, rather than calling remove on the list itself.
Originally posted by Jim Yingst:
[Dave]: Whak kind of array suits my needs better ?
To be clear, this question is not about arrays - it's about Lists.
[EFH]: The catch is, then, that calling get() on a LinkedList is slow!
Not really - not if you're we're talking about the beginning or end of the list. The get() method requires time proportional to the distance from either end of the list (whichever is closer). So getting from the middle is slow (unless the list is short), but getting from the ends is quick. And yes, getFirst() and getLast() are even faster, but that difference is quite small. for most applications. I wouldn't bother using the LinkedList methods unless profiling told me that was really a bottleneck - I'd prefer to keep the standard List methods. Same for remove(0) vs. remove(), removeFirst() and removeLast() - the differences in performance are trivial.
Note that in JDK 5, LinkedList has been retrofitted to implement the new Queue interface. For this situation, from what we've seen in Dave's code, Queue is probably a better fit than List. And there are other implementations of Queue besides LinkedList. I'm not sure offhand which of these would be best, but I would think that using the Queue interface would give you better options than the List interface.
[Jeff]: In a dimly-lit corner of my brain, I seem to have the words "circular queue" lodged.
Yeah, that's probably a good choice here (especially based on the limit to the maximum size of the collection). I'm sure there are implementations floating around out there (not in J2SE but in 3rd-party libraries) - though I don't know any in particular to recommend them. And sure, it's possible to implement your own. But for the effort it would take to make it and test carefully and make sure it's really right, I'd certainly wait until I had an observable performance problem and I'd already tried all the existing options in the JDK.
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your databasehttps://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database