您可以創建自己的類MultivalueMap這樣的:
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
public class MultiValueMap<T1, T2> {
public Map<T1, List<T2>> map = null;
public MultiValueMap(){
this.map = new HashMap();
}
public void putList(T1 key, List<T2> list){
map.put(key, list);
}
public void put(T1 key, T2 value){
List<T2> list = null;
if(map.get(key) == null){
list = new ArrayList<T2>();
map.put(key, list);
}
else {
list = map.get(key);
}
list.add(value);
}
public List<T2> get(T1 key){
return map.get(key);
}
public Set<T1> keySet(){
return map.keySet();
}
public Map getMap(){
return this.map;
}
public boolean contains(T1 key, T2 listValue){
List<T2> list = map.get(key);
return list.contains(listValue);
}
}