Which collection to choose?
I'm unsure of what collection to use in my program.
These are my desired characteristics:
- synchronized (I guess all of them can be);
- easy addition/removal the beginning and end
- easy merging of two collection objects;
- sorted;
- fast scrolling at any place.
The idea is to store data received from a database and use it in several resources (plots, tables, diagrams...). The data is sorted by time and will have several continuous chunks of sorted and ordered data (I can have plots with different time ranges, possibly intersecting each other).
My initial idea is to have a 2D collection/s. The main one to store the several continuous chunks, and the internal one with the sorted continuous data.
Any ideas on the best implementation?
[805 byte] By [
Fastfoxa] at [2007-11-15]

> If you want good thread support, use Vector. Pretty
> sure it meets all your criteria.
Hmm:
- synchronized (I guess all of them can be);
Yes
- easy addition/removal the beginning and end
No
- easy merging of two collection objects;
Yes
- sorted;
No
- fast scrolling at any place.
Yes
3 out of five isn't exactly 'all' the criteria.
I would use a TreeSet or a LinkedList myself.
I wouldn't suggest using Vector at all. Use ArrayList in preference to Vector. It is practically the same. If you want it synchronized, then use the Collections.synchronized* methods.
Your requirements are kinda conflicting there. You want easy addition/removal at end AND you want it sorted? They seem a bit contradictory to me.
Addition/removal of elements at the end of a Vector occurs at O(1) complexity, except during a resize operation (resizing the internal array, during expansion).
However I agree that addition/removal at the beginning may be linearly complex.
Please correct me if I am wrong.
Regards,
Kumar.
> I would use a TreeSet or a LinkedList myself.
It sounds to me like a LinkedList is a simple and nice solution. Provided, as has been hinted to, that you're sure your insertions at the beginning and end don't conflict with the sorting.
Now we're at the nitpicking, if you use a Vector and know the maximum capacity requirement from the outset, you can delete and insert at the end in O(1) time. Deleting and inserting at the beginning definitely still takes O(N), which is enough to state that this point in your requirements is not met.
More nitpicking, sorry. :-)
If you don't give a capacityIncrement for your Vector, it will double its capacity whenever it needs to grow. I believe that in the long run, this gives O(log(N)) insertion times when adding at the end even if you didn't know the maximum capacity requirement from the outset.
> If you want good thread support, use Vector. Pretty
> sure it meets all your criteria.
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vect
> or.html
>
> slegge
>
> --
> If you like my answer, please don't forget to award
> me your Duke points.
Don't use Vector.
This is an old object with many inconvenients.
Use Collections#synchronizedList(ArrayList).