views:

58

answers:

3

Hi,

Does anyone know a double entry table implementation in java I can download ?

I need to do something like this

   1  2  3
   _______
a| x  y  z 

b| h  l  m

c| o  a  k

table.get(a,1) would return x

Of course, it should use any Object as key, value, etc

Thanks in advance

+3  A: 

There are two basic approaches, depending on your needs.

One is to make a Hashtable (or similar) of Hashtables.

Hashtable<Integer, Hashtable<String, String>> = ...;

Another approach is to build your own datatype that represents an (Integer, String) pair, so you can do:

Hashtable<YourFancyDatatype, String>
VoteyDisciple
+1 dang, beat me to it
Jacob
I would point that if you are using __YourFancyDataType__ you will have to provide consistent implementation of __hashCode__ and __equals__ method.
Jack
In Java hash table is Hashtable and not HashTable.
Steve Kuo
@Jack, not unless YourFancyDataType only has once instance per value (like an enum).
Steve Kuo
Why use a `Hastable` and not a `HashMap`?
abhin4v
Any dictionary-style datatype would work; it's just a question of which interface is most appropriate for the particular operations you expect to be performing.
VoteyDisciple
A: 

I'm assuming you have an array of characters/objects and a quantity and want cross each other in your table. You could map each character to a number between 0 .. qtyOfCharacters and simply create a bidimensional array Object[][] table = new Object[A][B], where A is the quantity of characters/object just mapped, and B is the quantity of columns.

To map the characters/object to numbers you should use a HashMap/HashTable.

The idea is that if you access the element at "a,3" you should write table[ charmap.get("a")][ 3]

Ismael
+1  A: 

The answer to your question partially lies in a previous questions on SO: http://stackoverflow.com/questions/779414/java-generics-pairstring-string-stored-in-hashmap-not-retrieving-key-value-pr

import java.lang.*; 
import java.util.*; 

public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB> > {
  protected final TYPEA Key_;
  protected final TYPEB Value_;

  public Pair(TYPEA key, TYPEB value) {
    Key_   = key;
    Value_ = value;
  }
  public TYPEA getKey() {
    return Key_;
  }
  public TYPEB getValue() {
    return Value_;
  }
  public String toString() {
    System.out.println("in toString()");
    StringBuffer buff = new StringBuffer();
    buff.append("Key: ");
    buff.append(Key_);
    buff.append("\tValue: ");
    buff.append(Value_);
    return(buff.toString() );
  }
  public int compareTo( Pair<TYPEA, TYPEB> p1 ) { 
    System.out.println("in compareTo()");
    if ( null != p1 ) { 
       if ( p1.equals(this) ) { 
          return 0; 
       } else if ( p1.hashCode() > this.hashCode() ) { 
          return 1;
       } else if ( p1.hashCode() < this.hashCode() ) { 
          return -1;  
       }
    }
    return(-1);
  }

  public int hashCode() { 
    int hashCode = Key_.hashCode() + (31 * Value_.hashCode());
    System.out.println("in hashCode() [" + Integer.toString(hashCode) + "]");
    return(hashCode);
  }

  @Override
  public boolean equals(Object o) { 
      System.out.println("in equals()");
      if (o instanceof Pair) { 
         Pair<?, ?> p1 = (Pair<?, ?>) o;
         if ( p1.Key_.equals( this.Key_ ) && p1.Value_.equals( this.Value_ ) ) { 
            return(true);
         }
      }
      return(false);
  }

  public static void main(String [] args) {
     HashMap< Pair<String, int>, String> table = new HashMap<Pair<String,int>, String>();
     table.put(new Pair<String, int>("a", 1), "x");
     table.put(new Pair<String, int>("a", 2), "y");
     table.put(new Pair<String, int>("a", 3), "z");
     table.put(new Pair<String, int>("b", 1), "h");
     table.put(new Pair<String, int>("b", 2), "l");
     table.put(new Pair<String, int>("b", 3), "m");
     table.put(new Pair<String, int>("c", 1), "o");
     table.put(new Pair<String, int>("c", 2), "a");
     table.put(new Pair<String, int>("c", 3), "k"); 

     String val = table.get(new Pair<String, int>("a", 1)); //val is x for this input pair
  }
}
Khnle