11 public HashMap(int initialCapacity) {
12 init(initialCapacity, 0.75f);
15 public HashMap(int initialCapacity, float loadFactor) {
16 init(initialCapacity, loadFactor);
19 private void init(int initialCapacity, float loadFactor) {
20 table=new HashEntry[computeCapacity(initialCapacity)];
21 this.loadFactor=loadFactor;
23 this.threshold=(int)(loadFactor*table.length);
26 private static int computeCapacity(int capacity) {
33 private static int hash(Object o, int length) {
34 int orig=o.hashCode();
35 orig=orig^(orig>>>22)^(orig>>>10);
36 orig=orig^(orig>>>8)^(orig>>4);
37 return orig&(length-1);
41 int newCapacity=table.length<<1;
42 HashEntry[] oldtable=table;
43 this.table=new HashEntry[newCapacity];
44 this.threshold=(int) (newCapacity*loadFactor);
46 for(int i=0; i<oldtable.length; i++) {
47 HashEntry e=oldtable[i];
49 HashEntry next=e.next;
50 int bin=hash(e.key, newCapacity);
59 for(int i=0;i<table.length;i++)
64 public boolean isEmpty() {
72 /* 0=keys, 1=values */
73 public HashMapIterator iterator(int type) {
74 return (new HashMapIterator(this, type));
77 Object remove(Object key) {
78 int bin=hash(key, table.length);
79 HashEntry ptr=table[bin];
81 if (ptr.key.equals(key)) {
86 while(ptr.next!=null) {
87 if (ptr.next.key.equals(key)) {
88 Object oldvalue=ptr.value;
89 ptr.next=ptr.next.next;
99 Object get(Object key) {
100 int bin=hash(key, table.length);
101 HashEntry ptr=table[bin];
103 if (ptr.key.equals(key)) {
111 boolean containsKey(Object key) {
112 int bin=hash(key, table.length);
113 HashEntry ptr=table[bin];
115 if (ptr.key.equals(key)) {
123 Object put(Object key, Object value) {
125 if (numItems>threshold) {
129 int bin=hash(key, table.length);
130 HashEntry ptr=table[bin];
132 if (ptr.key.equals(key)) {
133 Object oldvalue=ptr.value;
139 HashEntry he=new HashEntry();