commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <bay...@generationjava.com>
Subject RE: [Collections] Insertion Ordered Map
Date Sun, 04 Nov 2001 21:50:06 GMT


On Sun, 4 Nov 2001, Lavandowska wrote:
> > The first is just a passing mention of SequencedHashMap, from the
> > Commons.Collections package, it appears to be a solution to some of
>
> SequencedHashMap fails in that the Set it returns for keySet does not
> maintain insertion order (believe me, I've tried it).

Yeah, I think you're meant to call sequence(). Not really perfect.
>
> > The second is why all the panic about the Hash part of it. My view of
> > extended Collection classes has always been to make a ProxyMap class,
> > which does nothing but delegate to another Map, and implement Map,
>
> I'm not sure if your suggestion would fit or not, but my particular
> concern is a case where a third-party piece of software requires a
> HashMap, but I want to use one that maintains insertion order (not
> something currently supported) rather than random or Natural ordering.
>
> If your ProxyMap solution can provide for both these cases (can be
> recognized as a HashMap and supports "arbitrary ordering") I would
> cheer the flexibility you propose.

Ugh. I hate APIs that enforce a need for HashMap rather than just Map :)

The only solution that leaps out to me is a HashMap which just proxies to
a Map, ie) a HashMapAdaptor. This seems to be sending us on swings nd
roundabouts a bit, but I still prefer it to effectively declaring all
Commons.Collections Map classes to be HashMap and not be chainable.

The other big problem is the single-inheritence of Java. One of my Map
extensions is TypedMap, which enforces that only certain types are added
to the Map. This currently extends AbstractTypedStructure and implements
Map, but I would really like to extend ProxyMap as well. The only obvious
design change is to encapsulate a TypeEnforcer or something and change the
extension.

In the end, the ProxyMap/ProxyList etc bit is less important than making
sure that the user of a class can decide whether to use a
HashMap/TreeMap/Other depending on the situation. One of the reasons
against this is that it allows the user to make a HashMap, tell the class
to use said HashMap, and then change the HashMap without going via the
class, but that's a good thing I think :) Allowing it anyway. Another is
that the initial Map/List may have contents already, so the class has to
decide how to deal with that.

Anyways, my proposed code for you to do your Ordered-by-insertion-map
would be:

HashMap hashmap = new HashMapAdaptor( new OrderedMap( new HashMap() ) );

I don't know how ugly that looks to you, if we say that OrderedMap is
FIFO, then FILO could be done as:

HashMap hashmap = new HashmapAdaptor( new ReversedMap( new OrderedMap( new
HashMap() ) ) );

Lastly, I'll attach a ListMap I have which is Yet Another Ordered Map.
Never know if it will be of any use.

Bay


Mime
View raw message