2

In Java Map interface what kind of flexibility does this declaration provide?

   V get(Object key);

   V put(K key, V value);

The generics has already put a restriction that only type K can be put in the map then what is the use of being able to get a type Object?

I can not see any point in getting null at run time if it happens to be of some other type. Why not get an error at compile time when trying to get some other type by declaring it as V get(Key key).

Can anyone share a practical scenario where this would be useful?

rakesh99
  • 1,234
  • 19
  • 34

0 Answers0