我有疑问,我想开发一个关于扩展hashmap的程序来添加putchildren方法.. 我写了主类,但现在我想写putChildrenValue方法.. 我的问题是: 我需要实现一个带有3个参数的putChildrenValue方法,String key,String key,ObjectValue。它将相应地存储系统。
当我完成此方法时完成方法数据
Key1 = "RUBY" value=HashMap which has -> "key2" = 5248 && "VALUE" = German
Key1 = "PYTHON" value=HashMap which has -> "key2" = 1234 && -> "VALUE" = German
我的主要课程是:
public static void main(String [] args)
{
ExtendedHashMap extendedMap = new ExtendedHashMap();
extendedMap.put (“Row1”, “Column1”, “German”);
extendedMap.put (“Row1”, “Column2”, “English”);
extendedMap.put (“Row1”, “Column3”, “Spanish”);
extendedMap.put (“Row2”, “Column1”, “Ruby”);
extendedMap.put (“Row2”, “Column2”, “Python”);
extendedMap.put (“Row3”, “Column3”, “Java”);
}
任何人都可以帮助我吗?
答案 0 :(得分:4)
可能会这样:
import java.util.HashMap;
public class ExtHashMap<K1, K2, V> extends HashMap<K1, HashMap<K2, V>> {
public ExtHashMap() {
super();
}
public void putChildrenValue(K1 key1, K2 key2, V value) {
HashMap<K2, V> childMap = get(key2);
if (childMap == null) {
childMap = new HashMap<K2, V>();
put(key1, childMap);
}
childMap.put(key2, value);
}
}
}
以下代码段提供了有关如何创建和使用它的示例:
ExtHashMap<String, int, String> map = new ExtHashMap<String, int, String>();
map.putChildValue("RUBY", 1234, "VALUE1");
map.putChildValue("PYTHON", 4321, "VALUE2");
子图按需自动处理。