I currently have
class A {...}
interface B {...}
What I would like to have is some equivalent of
HashMap<String, A implements B>
i.e., that a String
maps to any instance of A
that implements B
. But that syntax doesn't compile. Is there any way to map to a class that implements an interface? Note also that A
is not generic, so I can't do something like
A<? implements B>
There are three classes to implement maps. These three classes are HashMap, LinkedHashMap, and TreeMap.
1. Map implements collection interface? Explanation: Collection interface provides add, remove, search or iterate while map has clear, get, put, remove, etc. 2.
Any class that inherits a parent class, or implements an interface is a "Polymorph" of the parent class / interface.
To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.
It is possible to be done in a generic context, for instance, in a generic method:
public <T extends A & B> void method() {
Map<String, T> map = new HashMap<>();
}
Unfortunately, we can't apply these multiple restrictions to wildcards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With