Custom or vanilla Collection/Map?

V

VisionSet

I have a several classes that implement an interface that declares a
getInteger() method.

I would like to store objects of this type in some sort of collection

I require to iterate them in the order inserted and to get them based on
their Integer key.

LinkedHashMap I believe is the daddy.

however I'm thinking this is quite expensive and most of the time my
Integers will be consecutive, so why not reimplement a custom ArrayList to
do the job, that has offset ability.

MyArrayList could even determine as the objects are added if they have
consecutive integers, thus making it very efficient to find them via get(int
index). Otherwise it could revert to searching for them by iteration. (or
even using the LinkedHashMap).

So I guess my question is, how does this 2nd fall back approach compare to
LinkedHashMap?

Also how does a Map work? how does it do get(Object key)? does it iterate
its contents?

Another question which relates to testing stuff like this. I've seen in a
recentish post that it is possible to do unit tests that time very
accurately, ie better than a simple System.getCurrentTimeMillis() type
approach. Is the answer - get JUnit? Or can I do this simply without it?
 
R

Roedy Green

So I guess my question is, how does this 2nd fall back approach compare to
LinkedHashMap?

Linked HashMap just threads the objects insertion order so you can get
them in the original order. The lookup is identical to a HashMap.
 
R

Roedy Green

Also how does a Map work? how does it do get(Object key)? does it iterate
its contents?

Map is just an interface. It has nothing to say about how it works
inside.
 
H

Harald Hein

VisionSet said:
however I'm thinking this is quite expensive and most of the time
my Integers will be consecutive, so why not reimplement a custom
ArrayList to do the job, that has offset ability.

Do you need the performance? Then do it. If you don't need the
performance, don't do it. You don't know if you need the performance
(now)? Don't do it (now).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top