I am trying to understand which one is better than other. I mean for our project, performance is NOT AN ISSUE, I know. But as I read through the API for the appropriate one for our project, I could'nt deciede.
I used Vectors first, but later changed to ArrayList b'se of synchronization issues in Vector. Then again, I went through K&B book for the pros and cons of each collection class.
I found the following diff. between ArrayList and LinkedList in the book.
For faster iterations, use ArrayList. If you have more additions and removals, then prefer LinkedList over ArrayList
But could'nt find how to compare between HashSet and List classes.
Actually, I may change some from ArrayList to LinkedList in my program depending on the requirements. Its ok to use different set of collection classes right?
And I also read the HashSet API. Here is one important point, atleast I think, to consider.
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
Is it preferable to go for HashSet instead of ArrayList or LinkedList? I understood from above that as the size increases there will be overhead if we go for HashSet right?
Appreciate your help, thanks
The difference in iteration speed between LinkedList and ArrayList is probably very slight. Where ArrayList outperforms LinkedList for sure is when you access your List by index, and it's due to the fact that ArrayList uses an internal array.
There is no good or bad collection. The right one is just the one which better fits for your particular needs. And that's what's lacking in your post: for which use are you hesitating? A locks container? A cache (full or partial)? Another use?...
Giving your needs and the features of each collection (unicity or not, key/value pairs or not, access by index or not, ordered or not, sorted or not, etc...), it's generally easy to choose the best one.
My first use is
1. To hold the record values in static members and
2. For method local operations on the record values(mainly iterations).
I want to go for ArrayList for the second operation as its perfect for faster iterations.
And for one, I think both ArrayList or LinkedList should do. I am more inclined to use ArrayList as read/update/find operations will be more in general compared to createNew or delete.
But Phil, how can I relate my requirements to HashSet. Or you think, keep things simple and go for either ArrayList/LinkedList?
I used ArrayList. I could not find the difference between ArrayList or HashSet in this situation.
By definition a Set cannot contain duplicate values. An array list can.
And since record numbers are unique, e.g. there can only ever be one record number 99 that is marked as deleted, a Set works nicely for this.
If you use an ArrayList you can, for instance, add the record number 99 twice by mistake. And then lets say a new record is created at number 99, so your remove the entry 99 from the deleted numbers cache. But only one of the 99's were remove. The other one is still there. So record 99 will be used again next time a new record is created, and the previous record will be lost.
If you use a Set, that won't happen because there can be only one 99.
Hope that is helpful.