简体   繁体   English

我可以使用变量类型在Java中声明另一个变量吗?

[英]Can I use type of a variable to declare another variable in Java?

Can I do something like this in Java? 我可以在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>>();

or something like this 或类似的东西

HashMap<String, HashMap<String, Child>> parent1=new HashMap<String, HashMap<String, Child>>();
parent1.typeName parent2=new parent1.typeName;

Because some time, if the map level is too deep or too complex, it is very hard to write and read. 因为有一段时间,如果地图级别太深或太复杂,则很难写入和读取。

Abbreviations are possible by defining a subclass: 通过定义子类可以缩写:

class Str2Child extends HashMap<String, Child>>{}
class Str2Map extends HashMap<String,Str2Child>{}

Str2Map parent1 = new Str2Map();

No but you could shorten it if you're using Java 7 or higher. 不,但如果您使用的是Java 7或更高版本,则可以缩短它。 The compiler can infer the type parameters from the left side of the assignment and you can skip them altogether while creating the object HashMap<String, HashMap<String, Child>> parentMap = new HashMap<>(); 编译器可以从赋值的左侧推断出类型参数,你可以在创建对象时完全跳过它们HashMap<String, HashMap<String, Child>> parentMap = new HashMap<>();

In older versions of Java, you could resord to Guava's Maps class and its newHashMap method. 在旧版本的Java中,您可以使用Guava的Maps类及其newHashMap方法。 HashMap<String, HashMap<String, Child>> parentMap = Maps.newHashMap();

Another thing you could possibly do is create a type that implements a certain specification of the generic HashMap . 您可能要做的另一件事是创建一个实现泛型HashMap的特定规范的类型。

public class HashMapStringChild extends HashMap<String, Child> {
}

and then use it as a type parameter 然后将其用作类型参数

HashMap<String, HashMapStringChild> parent2 = new HashMap<>();

but personally, I find this a bit of a stretch. 但就个人而言,我觉得这有点紧张。 I certainly wouldn't overuse it and I'd be careful extending the collection classes. 我当然不会过度使用它,我会小心扩展集合类。

Addendum 附录

You should also note that you're effectively binding your API to a specific implementation of the Map interface ( HashMap ), or even worse, in case of introducing the new class ( HashMapStringChild ), to a specific, non-standard implementation. 您还应该注意,您有效地将API绑定到Map接口( HashMap )的特定实现,或者更糟糕的是,在将新类( HashMapStringChild )引入特定的非标准实现时。

What if at some point, you decide to keep your Child objects sorted at all times? 如果在某些时候,您决定始终对您的Child对象进行排序,该怎么办? You could do this by switching to a TreeMap but that would mean a big deal of refactoring. 您可以通过切换到TreeMap来实现这一点,但这意味着需要进行大量的重构。

You would be better off basing your API on a more general interface. 最好将API基于更通用的界面。 This way you could switch from 这样你可以切换

Map<String, Map<String, Child>> map = new HashMap<String, HashMap<String, Child>>();

to

Map<String, Map<String, Child>> map = new HashMap<String, TreeMap<String, Child>>();

or 要么

Map<String, Map<String, Child>> map = new TreeMap<String, TreeMap<String, Child>>();

or any other implementation without a hassle. 或任何其他没有麻烦的实施。

If you really want to make the map of String to Child a specific type, you could introduce an interface 如果您真的想将String to Child的映射设置为特定类型,则可以引入一个接口

public interface MapStringToChild extends Map<String, Child> {
}

Then you could keep your reference types general and use HashMap<String, Child> , TreeMap<String, Child> , HashMapStringChild or literally any other implementation mapping a String to a Child interchangeably, while keeping the code short. 然后你可以保持你的引用类型一般,并使用HashMap<String, Child>TreeMap<String, Child>HashMapStringChild或字面上将String映射到Child任何其他实现可互换,同时保持代码简短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM