Reputation: 11
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?
That is, I want to have something like
hashmap<<? implements A>, <? implements B>> _map;
_map.put(a1, b1);
where a1 implements A; and b1 implements 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.
Upvotes: 0
Views: 732
Reputation: 195199
this may be what you are looking for:
HashMap<A,B> map = new HashMap<A,B>();
map.put(a1,b1);
Upvotes: 0
Reputation: 137382
Yes it's possible, it would be enough to write:
Map<A, B> _map = HashMap<A, B>();
Upvotes: 0