Tuesday, March 22, 2011

MyHashMap


import java.util.*;
public class MyHashMap {

class HashEntry {
Object key;
Object val;
HashEntry(Object key, Object val) {
this.key=key; this.val=val;
}
public String toString() {return "{"+key+","+val+"}";}
public int hashCode() {if (key!=null) return key.hashCode(); return 0;}
public boolean equals(Object o) {
if (o==null) return false;
if (key==null && ((HashEntry)o).key==null) return true;
if (key!=null && ((HashEntry)o)!=null) return key.equals(((HashEntry)o).key);
return false;
}
}

private Set[] arr; // keep the set of HashEntry


public MyHashMap(int size) {
arr = new Set[size];
}

public MyHashMap() {
this(100);
}

private int computeHash(Object key, int size) {
int h = key.hashCode() % size;
if (h < 0) h = 0 - h;
return h;
}
/** assumes params are not null */
public Object get(Object key) {
if (key==null) return null;
int h = computeHash(key, arr.length);
if (arr[h]==null) return null;
for(Iterator i=arr[h].iterator(); i.hasNext(); ) {
HashEntry next = (HashEntry) i.next();
if (next.key.equals(key)) return next.val;
}
return null;
}
/** assumes params are not null */
public boolean put(Object key, Object value) {
if (key==null) return false;
if (value==null) return false;
HashEntry e = new HashEntry(key, value);
int h = computeHash(key, arr.length);
if (arr[h] == null) {
arr[h] = new HashSet();
}
if (arr[h].contains(e)) {
arr[h].remove(e);//remove old value
}
arr[h].add(e);
return true;
}

public String toString() {
StringBuffer b = new StringBuffer();
for(int i=0; i<arr.length; i++) {
b.append("arr["+i+"]:"+arr[i]);
b.append("\n");
}
return b.toString();
}

public static void main(String[] args) {
MyHashMap map = new MyHashMap(22);
map.put("bob the builder", new Integer(77));
map.put("TomTom", new Integer(99));
map.put("Gale", new Integer(1));
map.put("birdy 23", new Integer(-3));
map.put("Food for thought", new Integer(55));
map.put("A", new Integer(4));
map.put("0123", new Integer(44));
map.put("7", new Integer(-8));
map.put("I am me", new Integer(-77));
map.put("other", new Integer(1000));
map.put("some", new Integer(100));
map.put("THING", new Integer(10));
map.put("that", new Integer(1));
map.put("can", new Integer(11));
map.put("be", new Integer(18));
map.put("HERE", new Integer(2));
map.put("IS GOING TO BE HERE", new Integer(-9999));
System.out.println("map...");
System.out.println(map.toString());
System.out.println("get('7'): "+map.get("7"));
System.out.println("get('bob the builder'): "+map.get("bob the builder"));
map.put("bob the builder", new Integer(177));
System.out.println("get('bob the builder'), is now: "+map.get("bob the builder"));

System.out.println("get('HERE'): "+map.get("HERE"));
map.put("HERE", new Integer(111111111));
System.out.println("get('HERE'), is now: "+map.get("HERE"));

System.out.println("get('TomTom'):"+map.get("TomTom"));
System.out.println("get('Not in HashMap'):"+map.get("Not in HashMap"));

System.out.println("add 50 Integer values...");
for(int i=0; i<50; i++) {
map.put("i:"+i, new Integer(i));
}
System.out.println("map...");
System.out.println(map.toString());
System.out.println("\nshow the 50 Integer values backwards...");
for(int i=49; i>=0; i--) {
System.out.print(":"+ map.get("i:"+i));
}
}
}


map...
arr[0]:null
arr[1]:null
arr[2]:null
arr[3]:null
arr[4]:[{can,11}]
arr[5]:null
arr[6]:[{IS GOING TO BE HERE,-9999}]
arr[7]:null
arr[8]:null
arr[9]:null
arr[10]:[{other,1000}, {Food for thought,55}]
arr[11]:[{7,-8}, {I am me,-77}]
arr[12]:[{HERE,2}, {some,100}]
arr[13]:[{birdy 23,-3}, {Gale,1}]
arr[14]:[{TomTom,99}]
arr[15]:[{that,1}, {be,18}]
arr[16]:[{0123,44}]
arr[17]:null
arr[18]:[{THING,10}]
arr[19]:null
arr[20]:null
arr[21]:[{A,4}, {bob the builder,77}]

get('7'): -8
get('bob the builder'): 77
get('bob the builder'), is now: 177
get('HERE'): 2
get('HERE'), is now: 111111111
get('TomTom'):99
get('Not in HashMap'):null
add 50 Integer values...
map...
arr[0]:[{i:32,32}]
arr[1]:[{i:33,33}]
arr[2]:[{i:10,10}, {i:34,34}]
arr[3]:[{i:11,11}, {i:35,35}]
arr[4]:[{can,11}, {i:12,12}, {i:36,36}]
arr[5]:[{i:13,13}, {i:37,37}]
arr[6]:[{i:14,14}, {i:38,38}, {IS GOING TO BE HERE,-9999}]
arr[7]:[{i:15,15}, {i:40,40}, {i:39,39}]
arr[8]:[{i:16,16}, {i:41,41}]
arr[9]:[{i:17,17}, {i:42,42}]
arr[10]:[{other,1000}, {i:43,43}, {i:18,18}, {Food for thought,55}]
arr[11]:[{i:0,0}, {7,-8}, {i:44,44}, {i:20,20}, {i:19,19}, {I am me,-77}]
arr[12]:[{i:21,21}, {i:45,45}, {HERE,111111111}, {i:1,1}, {some,100}]
arr[13]:[{birdy 23,-3}, {i:22,22}, {i:46,46}, {i:2,2}, {Gale,1}]
arr[14]:[{i:23,23}, {TomTom,99}, {i:47,47}, {i:3,3}]
arr[15]:[{that,1}, {i:24,24}, {i:48,48}, {i:4,4}, {be,18}]
arr[16]:[{i:25,25}, {i:5,5}, {0123,44}, {i:49,49}]
arr[17]:[{i:26,26}, {i:6,6}]
arr[18]:[{i:27,27}, {THING,10}, {i:7,7}]
arr[19]:[{i:28,28}, {i:8,8}]
arr[20]:[{i:30,30}, {i:9,9}, {i:29,29}]
arr[21]:[{i:31,31}, {A,4}, {bob the builder,177}]


show the 50 Integer values backwards...
:49:48:47:46:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:2
3:22:21:20:19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0

No comments:

Post a Comment