class KeyValuePair { private Object key; private Object value; public KeyValuePair(Object key, Object value) { this.key=key; this.value=value; } public void putVal(Object v) { value=v; } public void putKey(Object k) { key=k; } public Object getVal() { return value; } public Object getKey() { return key; } public String toString() { return "("+key+","+value+")"; } } class myHashMap { private KeyValuePair[] hashmap; private int count; private final int SIZE; public myHashMap(int size) { hashmap = new KeyValuePair[size]; SIZE = size; for (int i=0 ; i < SIZE ; i++) hashmap[i]=new KeyValuePair(null,null); } public Object put(Object key, Object value) { if (count >= SIZE) return null; for (int i=0 ; i < SIZE ; i++) if (hashmap[i].getKey() != null) if (hashmap[i].getKey().equals(key)) { Object old = hashmap[i].getVal(); hashmap[i].putVal(value); return old; } hashmap[count].putKey(key); hashmap[count].putVal(value); count++; return value; } public Object get(Object key) { for (int i=0 ; i < SIZE ; i++) if (hashmap[i].getKey() != null) if (hashmap[i].getKey().equals(key)) return hashmap[i].getVal(); return null; } public String toString() { String pairs="{"; for (int i=0 ; i < SIZE ; i++) { pairs += hashmap[i]; if (i < SIZE-1) pairs += ","; } return pairs+"}"; } } public class myHashMapTest { public static void main(String[] args) { String[] keys = {"A","B","C"}; int[] vals = {1, 2, 3}; myHashMap stuff = new myHashMap(3); for (int i=0 ; i < keys.length ; i++) stuff.put(keys[i],new Integer(vals[i])); for (int i=0 ; i < keys.length ; i++) System.out.print(stuff.get(keys[i])+" "); System.out.println(); System.out.println(stuff); } }