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 LongHashMap { 56 57 int maxBuckets = 0; 58 Bucket[] buckets; 59 60 public LongHashMap() { 61 this(89); 62 } 63 64 public LongHashMap(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 LongHashMap(Bucket[] buckets) { 73 this.buckets = buckets; 74 this.maxBuckets = buckets.length; 75 } 76 77 public void put(long key, Object object) { 78 int index = (int) Math.abs(key % maxBuckets); 79 buckets[index].put(key, object); 80 } 81 82 public Object get(long key) { 83 int index = (int) Math.abs(key % maxBuckets); 84 return buckets[index].get(key); 85 } 86 87 public Object remove(long key) { 88 int index = (int) Math.abs(key % maxBuckets); 89 return buckets[index].remove(key); 90 } 91 92 public void print() { 93 for (int i=0; i<maxBuckets; i++) { 94 System.out.println("Bucket[" + i + "]: " + buckets[i]); 95 } 96 } 97 98 public static void main(String [] args) { 99 100 int count = 20; 101 long time=0, t1=0, t2 = 0; 102 String data = "SomeData_"; 103 104 LongHashMap map = new LongHashMap(); 105 for (long i=0; i<count; i+= 5) { 106 map.put(i, data + i); 107 } 108 109 for (long i=1; i<count; i+= 3) { 110 map.put(i, data + i); 111 } 112 113 for (long i=3; i<count; i+= 4) { 114 map.put(i, data + i); 115 } 116 117 for (long i=-23; i<count; i+= 4) { 118 map.put(i, data + i); 119 } 120 121 for (long j=-25; j<25; j++) { 122 System.out.println("Key: " + j + "; val: " + map.get(j)); 123 } 124 t2 = System.currentTimeMillis(); 125 126 } 127 128 } | Popular Tags |