码迷,mamicode.com
首页 > 其他好文 > 详细

JDK 源码解读之 Set接口

时间:2017-08-01 19:28:18      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:ace   racket   lock   []   源码解读   ring   efi   nal   follow   

public interface Set<E> extends Collection<E> {    //Set很简单,扩展了Collection

  Set<E> 和 Collection<E> 都带了<E>,是泛型接口。什么是泛型类,泛型接口呢?Oracle官方文档上是这么说的:

  A generic class is defined with the following format:

    class name<T1, T2, ..., Tn> { /* ... */ }

  The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variablesT1T2, ..., and Tn.

  泛型类形式上就是 类名<泛型类型>。

  A generic type is a generic class or interface that is parameterized over types.

 

  接下来,让我们再看另一个方法:

Iterator<E> iterator();

  iterator方法,返回的是一个Iterator<E>,也是一个泛型接口。

    <T> T[] toArray(T[] a);

  <T> 是一个generic Type,比如Integer、String,toArray() 的参数和返回值,都是T类型的数组。

接下来,我们再看另外两个方法:

  

    boolean addAll(Collection<? extends E> c);

    boolean retainAll(Collection<?> c);

<? extends E> 和 <?> 有什么不同呢?

  ?是一个通配符,代表unknown Type。

  <?> 代表 unbounded wildcard 。<? extends E> 代表upper bounded wildcards。

  List<Object> 与 List<?> 是相同的吗?

 

 default Spliterator<E> spliterator() {

这个方法,涉及到了java8的Stream API,等以后再说吧,现在超出我的范围了。。。。。

 

JDK 源码解读之 Set接口

标签:ace   racket   lock   []   源码解读   ring   efi   nal   follow   

原文地址:http://www.cnblogs.com/goodearth/p/7269854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!