[英]Hashmap generic
I want to create a hashmap where the key is of interface A, and the value is of interface B. Then I want to initialize it with classes that implements A and B. Is it possible to do it with java generics? 我想创建一个哈希映射,其中键是接口A的值,值是接口B的。然后,我想用实现A和B的类对其进行初始化。是否可以用Java泛型来实现?
That is, I want to have something like 也就是说,我想要类似
hashmap<<? implements A>, <? implements B>> _map;
_map.put(a1, b1);
where a1 implements A; 其中a1实现A; and b1 implements B.
b1实现B。
The original intent is that I want to create a factory, so that I can look up on a1 and return an instance of b1. 最初的目的是要创建一个工厂,以便可以查找a1并返回b1的实例。
Map<A, B> map = new HashMap<A, B>();
map.put(a1, b1);
是的,有可能,这样写就足够了:
Map<A, B> _map = HashMap<A, B>();
this may be what you are looking for: 这可能是您要寻找的:
HashMap<A,B> map = new HashMap<A,B>();
map.put(a1,b1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.