1 23 24 29 42 43 48 49 package com.sun.enterprise.util.collection; 50 51 import java.util.HashMap ; 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 55 public class IntHashMap { 56 57 int maxBuckets = 0; 58 Bucket[] buckets; 59 60 public IntHashMap() { 61 this(89); 62 } 63 64 public IntHashMap(int maxBuckets) { 65 this.maxBuckets = maxBuckets; 66 buckets = new Bucket[maxBuckets]; 67 for (int i=0; i<maxBuckets; i++) { 68 buckets[i] = new SortedArrayListBucket(); 69 } 70 } 71 72 public IntHashMap(Bucket[] buckets) { 73 this.buckets = buckets; 74 this.maxBuckets = buckets.length; 75 } 76 77 public void put(int key, Object object) { 78 int index = Math.abs(key % maxBuckets); 79 buckets[index].put(key, object); 80 } 81 82 public Object get(int key) { 83 int index = Math.abs(key % maxBuckets); 84 return buckets[index].get(key); 85 } 86 87 public boolean containsKey(int key) { 88 return ( (null!=get(key) ? true: false)); 89 } 90 91 public Object remove(int key) { 92 int index = Math.abs(key % maxBuckets); 93 return buckets[index].remove(key); 94 } 95 96 public void print() { 97 for (int i=0; i<maxBuckets; i++) { 98 System.out.println("Bucket[" + i + "]: " + buckets[i]); 99 } 100 } 101 102 public IntHashMapIterator iterator() { 103 return new IntHashMapIterator(); 104 } 105 106 private class IntHashMapIterator 107 implements Iterator 108 { 109 int bucketIndex = 0; 110 Iterator iter = null; 111 112 IntHashMapIterator() { 113 iter = buckets[0].iterator(); 114 } 115 116 public boolean hasNext() { 117 if (iter.hasNext()) { 118 return true; 119 } 120 121 while (++bucketIndex < maxBuckets) { 122 iter = buckets[bucketIndex].iterator(); 123 if (iter.hasNext()) { 124 return true; 125 } 126 } 127 128 return false; 129 } 130 131 public Object next() { 132 return iter.next(); 133 } 134 135 public void remove() { 136 } 137 } 138 139 public static void main(String [] args) { 140 141 int count = 20; 142 long time=0, t1=0, t2 = 0; 143 String data = "SomeData_"; 144 145 IntHashMap map = new IntHashMap(); 146 for (int i=0; i<count; i+= 5) { 147 map.put(i, data + i); 148 } 149 150 for (int i=1; i<count; i+= 3) { 151 map.put(i, data + i); 152 } 153 154 for (int i=3; i<count; i+= 4) { 155 map.put(i, data + i); 156 } 157 158 for (int i=-23; i<count; i+= 4) { 159 map.put(i, data + i); 160 } 161 162 for (int j=-25; j<25; j++) { 163 System.out.println("Key: " + j + "; val: " + map.get(j)); 164 } 165 t2 = System.currentTimeMillis(); 166 167 168 Iterator iter = map.iterator(); 169 while (iter.hasNext()) { 170 System.out.println("Got: " + iter.next()); 171 } 172 173 } 174 175 } 176 | Popular Tags |