user760613
user760613

Reputation:

Java class definition generics

What is the meaning of

class MyMap<K, V> implements Map<Comparable<K>, V>

in class definition? I don't understand how MyMap<K, V> can be a valid implementation of Map<Comparable<K>, V> as MyMap needs K and V whereas Map needs Comparable<K> and V

Upvotes: 1

Views: 89

Answers (1)

aioobe
aioobe

Reputation: 421340

You're over-analyzing the declaration. K is just any type, and MyMap implements Map<Comparable<K>, V>.

[...] don't understand how MyMap<K, V> can be a valid implementation of Map<Comparable<K>, V> [...]

It can if you implement the methods required by Map<Comparable<K>, V>. In particular MyMap needs to implement a method with the following signature for instance:

public Set<Comparable<K>> keySet() {
    ...
}

Note that the above method declaration puts no constraint on K. In other words the class declaration should not be confused with

class MyMap<K extends Comparable, V> implements Map<K, V>

which means that K needs to be Comparable.

Upvotes: 2

Related Questions