Page 1 of 1

About question enthuware.ocpjp.i.v11.2.3064

Posted: Mon Jul 15, 2019 5:24 am
by arnaldo
Given:
System.getProperties() returns a Properties object. Properties class extends HashTable. HashTable class implements Map interface. Map has a method named entrySet(), which returns Set<Map.Entry<K,V>>. Therefore, x is typed to Map.Entry<Object, Object> here and m is typed to Object.
and
Map has a method named keySet(), which returns Set<K>. Therefore, x is typed to Object and so, x.length() will not compile. It will work if this is changed to :   System.out.println(x);

why in the first case x is typed to Map.Entry<Object, Object> while in the second case x is typed to Object (and not Set<Object>)?

Re: About question enthuware.ocpjp.i.v11.2.3064

Posted: Mon Jul 15, 2019 2:54 pm
by admin
entry set returns Set<Map.Entry<K,V>> ==> Map.Entry<Object, Object>
key returns Set<K> => Object.

Observe the type specification of both the sets. The first one is Map.Entry<K, V> and the second one is just K.