KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > collection > LongHashMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: TooManyTasksException.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/collection/LongHashMap.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:13 $
47  */

48  
49 package com.sun.enterprise.util.collection;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
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 JavaDoc object) {
78         int index = (int) Math.abs(key % maxBuckets);
79         buckets[index].put(key, object);
80     }
81     
82     public Object JavaDoc get(long key) {
83         int index = (int) Math.abs(key % maxBuckets);
84         return buckets[index].get(key);
85     }
86     
87     public Object JavaDoc 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 JavaDoc[] args) {
99         
100         int count = 20;
101         long time=0, t1=0, t2 = 0;
102         String JavaDoc 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