views:

246

answers:

3

Is there a collection class that has the LinkedHashMap quality of a predictable iteration order, but at the same time being indexable? LinkedHashMap implements a get() method, which returns the value object, but an indexof() method is not available. I'd like both.

Thanks.

A: 

A SortedMap (ConcurrentSkipListMap, TreeMap) has methods that will return submap views of all keys > (or <) the passed in key. So

index(key) == sortedMap.headMap(key).size();

Not likely to be terribly efficient.

Steve B.
A: 

Take a look at the Apache Commons collection classes. One of the BidiMap implementations might be what you want ... assuming that you don't literally want to index the map.

Stephen C
+1  A: 

I have such a thing, a Linked Tree Map free and unencumbered on my website. It sounds close to what you want.

Software Monkey