1

Is there a reason that put method is defined with Generics whereas get is with Object, why is get not define like get(K key) ?

put(K key, V value)

and

get(Object key)
Abidi
  • 7,846
  • 14
  • 43
  • 65

1 Answers1

1

The generic types exist on collections to keep the contents of the collection consistent. I.e. if you have a Map<String, String> then you don't want to somehow get an Integer in it somewhere.

The get method doesn't pose this problem, and the get uses an equality match against the key which doesn't necessarily have to be the same class. So making it generic isn't needed for the consistency issue, and is undesirable when it comes to equality matching against keys.

Aaron
  • 652
  • 4
  • 10