Implementing polymorphic Java interfaces with specific types
I want to implement the Map<K,V>
interface开发者_运维技巧, but I want to constrain K
to type String
.
Is this possible? Or does the interface definition impose that K
be polymorphic?
If it is possible, could someone help with the class signature, tks.
try : class Test< V > implements Map< String, V >
hope it helps
No problem:
public class YourMap implements Map<String, Object> { /* ... */ }
If the class should be generic you can do:
public class YourMap<K extends String,V> implements Map<K, V> { /* ... */ }
But since String
is final (as @Peter Lawrey commented), you might as well just do:
public class YourMap<V> implements Map<String, V> { /* ... */ }
精彩评论