做学术搞研究的,很多时候还是要讲究个追根问底的,在网上看了很多关于List、Set、Collection、Iterable的相关知识,但是都很偏应用级,大致就是说他们是接口,是用来做什么的就没了。。。。就没了?我就觉得很不严谨,虽然我也不是很严谨,但下面我将以一种比较有迹可循的方式跟大家聊聊我对这些接口的理解
0. 什么是接口
简单说就是interface修饰的就是接口,然后可以在里面写抽象方法和全局常量,然后Java8中还引入了默认方法和静态方法。关于接口的使用,我下一期单独来说,这里就简单说一下为什么会有接口这个东西。
- 利于代码的规范:一方面为了给开发人员一个清晰的指示,告诉他们哪些业务(函数)需要实现;其次防止开发人员随意命名导致命名不清晰和代码混乱,影响开发效率。
- 有利于代码进行维护:在一开始的时候定义一个借口,把功能菜单放在接口里,然后定义类时实现这个接口,以后要换的话只不过是引用另一个类而已,其他代码都不用改变,这样就达到维护、拓展的方便性了。
- 保证代码的安全和严密:一个好的程序一定符合高内聚低耦合的特征,能够让系统的功能较好地实现,而不涉及任何具体的实现细节。这样就比较安全、严密一些,这一思想一般在软件开发中较为常见。
说了这么多,接口最直接的功能应该就是规范代码了,所以List、Set、Collection、Iterable其实质其实是一种规范。
1. 他们是什么关系
单纯从关系层面来讲,List和Set接口继承自Collection接口,然后Collection接口又继承自Iterable接口。

2. 通过源码注释看看他们是拿来规范什么的
2.1 Iterable
首先讲讲最顶层的Iterable
Implementing this interface allows an object to be the target of the "for-each loop" statement. 实现此接口的对象允许成为for-each循环语句的目标
意思是指,一个类实现了这个接口,那你这个类就要提供一个for-each循环函数,那循环是什么,什么时候需要用到循环呢?一个集合里面有很多个元素,你要一个个的看才会用到循环。所以,这个Iterable看名字意思好像是“可迭代的”,其实他是在声明一种规范——实现这个借口你就必须有可循环、可遍历功能。
2.2 Collection
这个的注释太多了,得注释加翻译得有个好几百字,我就不复制粘贴了,简单给大家说一下注释里面的关键词。
The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. 集合层次结构中的根接口。集合表示一组对象,称为元素。一些集合允许重复的元素另一些则不允许,一些是有序的另一些是无序的。
The JDK does not provide any direct implementations of this interface:it provides implementations of more specific subinterfaces like Set and List. JDK不提供Collection接口的任何实现,但提供特定的子接口Set和List
All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument. 所有的Collection实现类都应该提供两个“标准的”构造函数:其中一个无参的构造函数用于创建空集合,还有一个带有单个参数类型的构造函数用于创建一个新集合其元素类型与参数相同。
It is up to each collection to determine its own synchronization policy. 由每个集合决定自己的同步策略
大概说这几个方面,这只是Collection注释的三分之一吧,有兴趣的可以自己去看一看。
然后这些告诉我们什么呢,首先是引入了集合这个概念,并且Collection接口本身只用于定义集合这件事,而具体的规范由子接口Set、List来定义。其次是你可以创建空的集合也可以创建带参数的集合,带参数的集合就是在创建之初就规定好集合里可以放哪类元素了(泛型)。最后就是关于多线程同步的事Collection接口也不管,由各实现类自己解决。
2.3 Set
为什么先说Set呢,因为Collection里先提到Set,哈哈哈哈哈
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. 不包含重复元素的集合,更准确的说,集合不包含e1.equals(e2)为真的重复对象
The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. 除了从Collection接口继承的规定外,Set接口还对创建约束、add、equals、hashCode方法进行了额外的规定
The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements. 关于创建约束的额外规定是,创建的Set都必须是一个不包含重复元素的集合。
Some set implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. 一些Set的实现类可能包含元素限制,比如有的禁止null,也有一些实现对其元素类型有限制。
同样的,Set的注释也有很多,上面这几点已经可以把Set的规范说的很清楚了,基于equals判断的不可重复的元素集合,是的规范就这一句话,具体的内容还要到实现的时候才能知道。
2.4 List
终于快看完了,英文的注释看得我头疼。。。
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. 有序集合(也成为序列)。用户可以通过精确控制序列中每个元素的插入位置,用户可以通过整数索引访问元素,并在列表中搜索元素。
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. 与Set不同,List通常允许重复元素,且也是基于equals方法的,并且List如果允许空元素,则他可以有很多个空元素。
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. 除了从Collection接口继承的规定外,List接口还对iterator、add、remove、equals、hashCode方法附加了其他规定
还有很多关于List在设计方面和使用方面的注释,有空再给大家介绍。从上面三点可以看出List规范用于有序可重复集合,同时他还有迭代器。
3. 区别与关系
- 容器接口
- Collection继承自Iterable,所以Collection拥有Iterable的特性。
- Set和List继承自Collection所以他们拥有Collection和Iterable的特性。
- 实现了Set或List的类,需要对接口规范的方法进行逻辑处理,其中包含Set、Collection、Iterable或者是List、Collection、Iterable所提供的规范。
- 都继承自Iterable,所以他们都能遍历
- Iterable、Collection都不提供实现类,他们只提供规范,Set、List提供实现类(基于JDK)
- Set、List的实现类有哪些功能、哪些特性在接口层面看不出来
4. 思考
List和Set的区别一直存在于各企业的面试题库中,但你在网上搜索List和Set的区别,大多数人居然会简单直接地告诉你List是有序的,Set是无序的,然后给你说说ArrayList、Vector、LinkedList、HashSet的区别,但看看人官方Set的注释,压根儿就没说有序无序的事,而List实现的类必须是有序的,也可能跟他自身的工作环境有关,我当初刚入职也以为List就是有序集合,Set就是无序集合,因为大家就这么用的,我也没深究,这大概就是人云亦云、小马过河等故事的最好例子了。
所以说做学问搞技术要严谨,多看看人家官方文档,官方注释什么的,就比如我,哈哈哈哈哈,最后希望你能关注我,多看看我的文章,给我点赞,太惨了兄弟们,粉丝一个也没有,没粉丝我入职面试怎么跟人唠。(突然哭惨)