Re: How to code something in Java
ram@zedat.fu-berlin.de (Stefan Ram) writes:
map.put( p( 'a', 'b' ), 3 );
map.put( p( 'a', 'y' ), 5 );
java.lang.System.out.println( map.get( p( 'a', 'b' )));
java.lang.System.out.println( map.get( p( 'a', 'y' )));
One might remove the need for the above
call to ?p? to simplify the interface:
class P<E>
{ final E[] value;
public P( final E ... value ){ this.value = value; }
public int hashCode(){ return java.util.Arrays.hashCode( value ); }
public boolean equals( final java.lang.Object value )
{ return value instanceof P ? java.util.Arrays.equals
( this.value,(( P )value ).value ): false; }}
interface MultiMap<K,V>
{ public void set( final V value, final K ... keys );
public V get( final K ... keys ); }
class MultiHashMap<K,V> extends java.util.HashMap<P<K>,V>
implements MultiMap<K,V>
{ public void set( final V value, final K ... keys )
{ put( new P<K>( keys ), value ); }
public V get( final K ... keys ){ return get( new P<K>( keys )); }}
public class Main
{ static final MultiMap<java.lang.Character,java.lang.Integer> map
= new MultiHashMap<java.lang.Character,java.lang.Integer>();
public static void main( final java.lang.String[] args )
{
map.set( 3, 'a', 'b' );
map.set( 5, 'a', 'y' );
java.lang.System.out.println( map.get( 'a', 'b' ));
java.lang.System.out.println( map.get( 'a', 'y' )); }}
3
5