用 “ 集合框架 ” 设计软件时,记住该框架四个基本接口的下列层次结构关系会有用处:
- Collection 接口是一组允许重复的对象。
- Set 接口继承 Collection ,但不允许重复。
- List 接口继承 Collection ,允许重复,并引入位置下标。
- Map 接口既不继承 Set 也不继承 Collection , 存取的是键值对
我们以下面这个图表来描述一下常用的集合的实现类之间的区别:
Collection/Map
|
接口
|
成员重复性
|
元素存放顺序( Ordered/Sorted )
|
元素中被调用的方法
|
基于那中数据结构来实现的
|
HashSet
|
Set
|
Unique elements
|
No order
|
equals()
hashCode()
|
Hash 表
|
LinkedHashSet
|
Set
|
Unique elements
|
Insertion order
|
equals()
hashCode()
|
Hash 表和双向链表
|
TreeSet
|
SortedSet
|
Unique elements
|
Sorted
|
equals()
compareTo()
|
平衡树( Balanced tree )
|
ArrayList
|
List
|
Allowed
|
Insertion order
|
equals()
|
数组
|
LinkedList
|
List
|
Allowed
|
Insertion order
|
equals()
|
链表
|
Vector
|
List
|
Allowed
|
Insertion order
|
equals()
|
数组
|
HashMap
|
Map
|
Unique keys
|
No order
|
equals()
hashCode()
|
Hash 表
|
LinkedHashMap
|
Map
|
Unique keys
|
Key insertion order/Access order of entries
|
equals()
hashCode()
|
Hash 表和双向链表
|
Hashtable
|
Map
|
Unique keys
|
No order
|
equals()
hashCode()
|
Hash 表
|
TreeMap
|
SortedMap
|
Unique keys
|
Sorted in key order
|
equals()
compareTo()
|
平衡树( Balanced tree )
|
|