1 24 25 package com.mckoi.util; 26 27 import java.util.*; 28 29 36 37 public class HashMapList { 38 39 private static final List EMPTY_LIST = Arrays.asList(new Object [0]); 40 41 private HashMap map; 42 43 46 public HashMapList() { 47 map = new HashMap(); 48 } 49 50 53 public void put(Object key, Object val) { 54 ArrayList list = (ArrayList) map.get(key); 55 if (list == null) { 56 list = new ArrayList(); 57 } 58 list.add(val); 59 map.put(key, list); 60 } 61 62 66 public List get(Object key) { 67 ArrayList list = (ArrayList) map.get(key); 68 if (list != null) { 69 return list; 70 } 71 return EMPTY_LIST; 72 } 73 74 77 public boolean remove(Object key, Object val) { 78 ArrayList list = (ArrayList) map.get(key); 79 if (list == null) { 80 return false; 81 } 82 boolean status = list.remove(val); 83 if (list.size() == 0) { 84 map.remove(key); 85 } 86 return status; 87 } 88 89 93 public List clear(Object key) { 94 ArrayList list = (ArrayList) map.remove(key); 95 if (list == null) { 96 return new ArrayList(); 97 } 98 return list; 99 } 100 101 104 public Set keySet() { 105 return map.keySet(); 106 } 107 108 111 public boolean containsKey(Object key) { 112 return map.containsKey(key); 113 } 114 115 } 116 | Popular Tags |