public interface Map<K,V> 这是Map接口的声明~
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's own <tt>remove</tt> operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
* operations.
*
* @return a set view of the keys contained in this map
*/
Set<K> keySet(); //这是Map接口里面 keySet方法的声明
我觉得是否要写泛型只和函数的声明有关,函数返回值里面带了 <K>,如果不写泛型会有警告,但是不会报错。
而且~如果 Map<K,V> 泛型的 K 和 Set<K> keySet() 的 K 类型 不一致编译是不会过的~
下面的iterator是一个道理
|