ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### [简单集合分类](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=%e7%ae%80%e5%8d%95%e9%9b%86%e5%90%88%e5%88%86%e7%b1%bb) 可以看到,实际上只有四个基本的集合组件:**Map**,**List**,**Set**和**Queue**,它们各有两到三个实现版本(**Queue**的**java.util.concurrent**实现未包含在此图中)。最常使用的集合用黑色粗线线框表示。 虚线框表示接口,实线框表示普通的(具体的)类。带有空心箭头的虚线表示特定的类实现了一个接口。实心箭头表示某个类可以生成箭头指向的类的对象。例如,任何**Collection**都可以生成**Iterator**,**List**可以生成**ListIterator**(也能生成普通的**Iterator**,因为**List**继承自**Collection**)。 下面的示例展示了各种不同的类在方法上的差异。实际代码来自[泛型](https://lingcoder.gitee.io/onjava8/#/)章节,在这里只是调用它来产生输出。程序的输出还展示了在每个类或接口中所实现的接口: ~~~ // collections/CollectionDifferences.java import onjava.*; public class CollectionDifferences { public static void main(String[] args) { CollectionMethodDifferences.main(args); } } /* Output: Collection: [add, addAll, clear, contains, containsAll, equals, forEach, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray] Interfaces in Collection: [Iterable] Set extends Collection, adds: [] Interfaces in Set: [Collection] HashSet extends Set, adds: [] Interfaces in HashSet: [Set, Cloneable, Serializable] LinkedHashSet extends HashSet, adds: [] Interfaces in LinkedHashSet: [Set, Cloneable, Serializable] TreeSet extends Set, adds: [headSet, descendingIterator, descendingSet, pollLast, subSet, floor, tailSet, ceiling, last, lower, comparator, pollFirst, first, higher] Interfaces in TreeSet: [NavigableSet, Cloneable, Serializable] List extends Collection, adds: [replaceAll, get, indexOf, subList, set, sort, lastIndexOf, listIterator] Interfaces in List: [Collection] ArrayList extends List, adds: [trimToSize, ensureCapacity] Interfaces in ArrayList: [List, RandomAccess, Cloneable, Serializable] LinkedList extends List, adds: [offerFirst, poll, getLast, offer, getFirst, removeFirst, element, removeLastOccurrence, peekFirst, peekLast, push, pollFirst, removeFirstOccurrence, descendingIterator, pollLast, removeLast, pop, addLast, peek, offerLast, addFirst] Interfaces in LinkedList: [List, Deque, Cloneable, Serializable] Queue extends Collection, adds: [poll, peek, offer, element] Interfaces in Queue: [Collection] PriorityQueue extends Queue, adds: [comparator] Interfaces in PriorityQueue: [Serializable] Map: [clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, replace, replaceAll, size, values] HashMap extends Map, adds: [] Interfaces in HashMap: [Map, Cloneable, Serializable] LinkedHashMap extends HashMap, adds: [] Interfaces in LinkedHashMap: [Map] SortedMap extends Map, adds: [lastKey, subMap, comparator, firstKey, headMap, tailMap] Interfaces in SortedMap: [Map] TreeMap extends Map, adds: [descendingKeySet, navigableKeySet, higherEntry, higherKey, floorKey, subMap, ceilingKey, pollLastEntry, firstKey, lowerKey, headMap, tailMap, lowerEntry, ceilingEntry, descendingMap, pollFirstEntry, lastKey, firstEntry, floorEntry, comparator, lastEntry] Interfaces in TreeMap: [NavigableMap, Cloneable, Serializable] */ ~~~ 除**TreeSet**之外的所有**Set**都具有与**Collection**完全相同的接口。**List**和**Collection**存在着明显的不同,尽管**List**所要求的方法都在**Collection**中。另一方面,在**Queue**接口中的方法是独立的,在创建具有**Queue**功能的实现时,不需要使用**Collection**方法。最后,**Map**和**Collection**之间唯一的交集是**Map**可以使用`entrySet()`和`values()`方法来产生**Collection**。 请注意,标记接口**java.util.RandomAccess**附加到了**ArrayList**上,但不附加到**LinkedList**上。这为根据特定**List**动态改变其行为的算法提供了信息。 从面向对象的继承层次结构来看,这种组织结构确实有些奇怪。但是,当了解了**java.util**中更多的有关集合的内容后(特别是在[附录:集合主题](https://lingcoder.gitee.io/onjava8/#/)中的内容),就会发现除了继承结构有点奇怪外,还有更多的问题。集合类库一直以来都是设计难题——解决这些问题涉及到要去满足经常彼此之间互为牵制的各方面需求。所以要做好准备,在各处做出妥协。 尽管存在这些问题,但 Java 集合仍是在日常工作中使用的基本工具,它可以使程序更简洁、更强大、更有效。你可能需要一段时间才能熟悉集合类库的某些方面,但我想你很快就会找到自己的路子,来获得和使用这个类库中的类。 \[^1\]: 许多语言,例如 Perl ,Python 和 Ruby ,都有集合的本地支持。 \[^4\]:`remove()`是一个所谓的“可选”方法(还有一些其它的这种方法),这意味着并非所有的**Iterator**实现都必须实现该方法。这个问题将在[附录:集合主题](https://lingcoder.gitee.io/onjava8/#/)中介绍。但是,标准 Java 库集合实现了`remove()`,因此在[附录:集合主题](https://lingcoder.gitee.io/onjava8/#/)章节之前,都不必担心这个问题。 \[^5\]: 这实际上依赖于具体实现。优先级队列算法通常会按插入顺序排序(维护一个*堆*),但它们也可以在删除时选择最重要的元素。 如果对象的优先级在它在队列中等待时可以修改,那么算法的选择就显得很重要了。 \[^6\]: 有些人提倡这样一种自动创建机制,即对一个类中所有可能的方法组合都自动创建一个接口,有时候对于单个的类都是如此。 我相信接口的意义不应该仅限于方法组合的机械地复制,因此我在创建接口之前,总是要先看到增加接口带来的价值。 \[^7\]: 这在 Java 5 之前是不可用的,因为该方法被认为与操作系统的耦合度过紧,因此违反“一次编写,处处运行”的原则。现在却提供它,这一事实表明, Java 的设计者们更加务实了。 \[^8\]: 下面是译者绘制的 Java 集合框架简图,黄色为接口,绿色为抽象类,蓝色为具体类。虚线箭头表示实现关系,实线箭头表示继承关系。![collection](https://lingcoder.gitee.io/onjava8/images/collection.png)![map](https://lingcoder.gitee.io/onjava8/images/map.png)