我可以用 Java 做这样的事情吗?
HashMap<String, Child> childMap=new HashMap<String, Child>();
HashMap<String, childMap.typeName> parentMap=new HashMap<String, childMap.typeName>();
//instead of
HashMap<String, HashMap<String, Child>> parentMap=new HashMap<String, HashMap<String, Child>>();
或者类似的东西
HashMap<String, HashMap<String, Child>> parent1=new HashMap<String, HashMap<String, Child>>();
parent1.typeName parent2=new parent1.typeName;
因为有些时候,如果 map 层级太深或太复杂,写起来和读起来都非常困难。
最佳答案
可以通过定义子类进行缩写:
class Str2Child extends HashMap<String, Child>>{}
class Str2Map extends HashMap<String,Str2Child>{}
Str2Map parent1 = new Str2Map();
关于java - 我可以使用变量类型在 Java 中声明另一个变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028860/