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]
# 1
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/Vector.htmlslegge--If you like my answer, please don't forget to award me your Duke points.
sleggea at 2007-7-13 > top of java,Core,Core APIs...
# 2

> 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.

ejpa at 2007-7-13 > top of java,Core,Core APIs...
# 3
- easy addition/removal the beginning and endshould be a "Yes". Addition/removal of elements at the beginning/end is an O(1) operation.Hence Vector should get 4/5 :-)Regards,Kumar.
kumar_iyera at 2007-7-13 > top of java,Core,Core APIs...
# 4

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.

evnafetsa at 2007-7-13 > top of java,Core,Core APIs...
# 5
unless you can guarantee that entries are inserted only in an order that's already sorted (but in that case, why want the collection to be sorted in the first place?).
jwentinga at 2007-7-13 > top of java,Core,Core APIs...
# 6
> Addition/removal of elements at the beginning/end is an O(1) operation.On a Vector these operations are O(N).
ejpa at 2007-7-13 > top of java,Core,Core APIs...
# 7

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.

kumar_iyera at 2007-7-13 > top of java,Core,Core APIs...
# 8
You have to take the resize operations into account, and they are O(N), so the operations are O(N) by definition.
ejpa at 2007-7-13 > top of java,Core,Core APIs...
# 9

> 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.

OleVVa at 2007-7-13 > top of java,Core,Core APIs...
# 10

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.

OleVVa at 2007-7-13 > top of java,Core,Core APIs...
# 11

> 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).

ibanna at 2007-7-13 > top of java,Core,Core APIs...