Map to class implementing interface

I currently have

class A {...} interface B {...} 

What I would like to have is some equivalent

 HashMap<String, A implements B> 

ie, that a String maps to any instance of A that implements B But this syntax does not compile. Is there a way to map a class that implements an interface? Also note that A not generic, so I cannot do something like

 A<? implements B> 
+5
source share
1 answer

This can be done in a general context, for example, in a general method:

 public <T extends A & B> void method() { Map<String, T> map = new HashMap<>(); } 

Unfortunately, we cannot apply these multiple restrictions to wildcards.

+5
source

Source: https://habr.com/ru/post/1260718/


All Articles