KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > MultiHashMap


1 package com.jdon.util;
2
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Set JavaDoc;
6
7 /**
8  * MultiHashMap is a extension java.util.HashMap.It is usde if following condition.<br>
9  * Sometime, a value shoud be determinated by key and subkey.
10  */

11 public class MultiHashMap extends HashMap JavaDoc{
12
13     public MultiHashMap() {
14         super();
15     }
16
17     /**
18      * Associates the specified value with the specified key and subKey in this map.
19      * If the map previously contained a mapping for this key and subKey , the old value is replaced.
20      * @param key Is a Primary key.
21      * @param subKey with which the specified value is to be associated.
22      * @param value to be associated with the specified key and subKey
23      * @return previous value associated with specified key and subKey, or null if there was no mapping for key and subKey.
24      * A null return can also indicate that the HashMap previously associated null with the specified key and subKey.
25      */

26     public Object JavaDoc put(Object JavaDoc key,Object JavaDoc subKey,Object JavaDoc value){
27         HashMap JavaDoc a = (HashMap JavaDoc)super.get(key);
28         if(a==null){
29             a = new HashMap JavaDoc();
30             super.put(key,a);
31         }
32         return a.put(subKey,value);
33     }
34
35     /**
36      * Returns the value to which this map maps the specified key and subKey. Returns null if the map contains no mapping
37      * for this key and subKey. A return value of null does not necessarily indicate that the map contains no mapping
38      * for the key and subKey; it's also possible that the map explicitly maps the key to null.
39      * The containsKey operation may be used to distinguish these two cases.
40      * @param key whose associated value is to be returned.
41      * @param subKey whose associated value is to be returned
42      * @return the value to which this map maps the specified key.
43      */

44     public Object JavaDoc get(Object JavaDoc key,Object JavaDoc subKey){
45         HashMap JavaDoc a = (HashMap JavaDoc)super.get(key);
46         if(a!=null){
47             Object JavaDoc b=a.get(subKey);
48             return b;
49         }
50         return null;
51     }
52     
53     public Set JavaDoc getSubKeys(Object JavaDoc key){
54         HashMap JavaDoc a = (HashMap JavaDoc)super.get(key);
55         if(a==null) return null;
56         return a.keySet();
57     }
58     
59     
60 }
61
Popular Tags