Java 集合框架(接口)

核心集合接口概述

Java 集合框架的核心接口,主要由两颗树组成:java.util.Collection、java.util.Map
它们层次结构关系,如下图:


Java 集合框架核心接口

Java 官方文档中提到:

The core collection interfaces encapsulate different types of collections. These interfaces allow collections to be manipulated independently of the details of their representation. Core collection interfaces are the foundation of the Java Collections Framework. As you can see in the following figure, the core collection interfaces form a hierarchy.

大致是在说,这些核心集合接口:

  • 封装了不同类型的集合
  • 允许独立于其表示的细节来操纵集合(多态)
  • 是Java集合框架的基础

当然还提到了最重要的一点:

To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional — a given implementation may elect not to support all operations. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException. Implementations are responsible for documenting which of the optional operations they support. All of the Java platform's general-purpose implementations support all of the optional operations.

这里主要讲的是:
Java 平台为控制核心集合接口的数量,不会为特殊实现(即:变体)提供单独的接口,而是采取另一种方式:每个接口的修改方法都是可选的(optional)。
怎么理解呢,说白了,就是实现类在实现接口的时候,可以选择性的实现这些方法。若调用了没有实现的方法,就会抛出异常:UnsupportedOperationException。
(在提一点:在阅读源码的时候,如果你看到 optional operation 这样的文字,就是在说明,该方法是一个可选方法)


核心集合接口描述

Java 官方文档中的描述:

The following list describes the core collection interfaces:

  • Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List.
  • Set — a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine.
  • List — an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've used Vector, you're familiar with the general flavor of List.
  • Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
    Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. Whatever the ordering used, the head of the queue is the element that would be removed by a call to remove or poll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.
  • Deque — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Deque provides additional insertion, extraction, and inspection operations.
    Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends.
  • Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you've used Hashtable, you're already familiar with the basics of Map.

精简一下,差不多就这个意思:

  • Collection:集合层次结构的根。
  • Set:不能包含重复元素的集合。
  • List:有序集合(有时称为序列)。可以包含重复的元素。
  • Queues:用于在处理之前保存多个元素的集合。除了基本的收集操作之外,队列还提供额外的插入、提取和检查操作。Queues通常(但不一定)以FIFO(先入先出)的方式对元素进行排序。
  • Deque:用于在处理之前保存多个元素的集合。除了基本的收集操作之外,队列还提供额外的插入、提取和检查操作。Deques 可以同时用作FIFO(先入先出)和LIFO(后入先出)。在Deques中,所有新元素都可以在两端插入、检索和删除。
  • Map:将键映射到值的对象。Map 不能包含重复的键;每个键最多可以映射到一个值。

还有两个排序的版本:

The last two core collection interfaces are merely sorted versions of Set and Map:

  • SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
  • SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.

讲的是 Set 和 Map 的排序版本:

  • SortedSet:以升序维护其元素的集合。还提供了一些额外的操作来利用排序。排序集用于自然排序集,例如单词列表和成员列表。
  • SortedMap:以升序键顺序维护映射的映射。这是SortedSet的映射模拟。排序映射用于自然排序的键/值对集合。

更多参考:
Interfaces (The Java™ Tutorials > Collections)

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 8,139评论 0赞 10
  • 上个月的朋友聚会上,我一个曾经无比放飞自我的闺蜜抱怨初为人母完全没有育儿经验,可以说是跌跌撞撞走到现在,整个人完全...
    NJ安可阅读 719评论 0赞 1
  • 一天,课余时间和几位年轻教师坐在一起聊天。 “现在的孩子怎么不爱学习呢?” “是啊!现在的孩子看着老师,离开你的眼...
    书写美好的生活阅读 1,454评论 30赞 42
  • 老公语录1 2018年4月11日,星期四,天气:晴 老公今天对我说:“哟,婷丫子都没有穿秋裤了,说明夏天真滴快来了...
    虢长史阅读 179评论 0赞 0
  • 敏姐一口气说完,长叹一声,“姐的这些往事,从来就没对别人说过。姐也不怕你笑话,唉,这样的日子,啥时候是个头啊!” ...
    疯一样的绅士阅读 1,462评论 0赞 0

友情链接更多精彩内容