Re: How to use java.util.Map in a more Perl like way.
On Aug 3, 11:07 pm, Thomas Hawtin <use...@tackline.plus.com> wrote:
class MyMap extends Map
I guess that would be extends HashMap. Perhaps introduce an interface
that extends Map and a class that extends HashMap and implements the
interface.
Ugh!!
Try
public class MyMap<K,V> implements Map<K,V>
private Map<K,V> delegate;
public MyMap () {
delegate = new HashMap<K,V>();
}
public MyMap (Map<K,V> delegate) {
this.delegate = delegate;
}
...
}
This uses a HashMap by default but lets people use the alt constructor
to dependency-inject and get a MyMap based on a TreeMap, etc. (and of
course they can specify a TreeMap comparator when they construct the
TreeMap prior to passing it to the MyMap constructor). The one iffy
thing is the aliasing that occurs if the TreeMap (or whatever)
reference is kept around and also gets used; changes to the MyMap and
the TreeMap are reflected in one another.
To OP: If the key types where you want multiple keys are always all
the same, just use Map<List<KeyType>, ValueType>; this supports
different key list lengths for different entries. If you don't want
that make a fixed-size immutable list class; e.g. use
Map<ThreeElementList<KeyType>, ValueType> and make ThreeElementList a
class that implements List and has a constructor that accepts a List
but throws if it's the wrong length, otherwise wrapping it and
implementing none of the "optional" (list-mutating) operations.