Documentation

The Java™ Tutorials
Hide TOC
Interfaces接口
Trail: Collections

Lesson: Interfaces课程:接口

The core collection interfaces encapsulate different types of collections, which are shown in the figure below. 核心集合接口封装了不同类型的集合,如下图所示。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. 核心集合接口是java集合框架的基础。As you can see in the following figure, the core collection interfaces form a hierarchy.如下图所示,核心集合接口构成了一个层次结构。

Two interface trees, one starting with Collection and including Set, SortedSet, List, and Queue, and the other starting with Map and including SortedMap.

The core collection interfaces.核心集合接口。

A Set is a special kind of Collection, a SortedSet is a special kind of Set, and so forth. Set是一种特殊的CollectionSortedSet是一种特殊的Set,依此类推。Note also that the hierarchy consists of two distinct trees — a Map is not a true Collection.还要注意,层次结构由两个不同的树组成—Map不是真实的Collection

Note that all the core collection interfaces are generic. 请注意,所有核心集合接口都是通用的。For example, this is the declaration of the Collection interface.例如,这是Collection接口的声明。

public interface Collection<E>...

The <E> syntax tells you that the interface is generic. 这个<E>语法告诉您接口是通用的。When you declare a Collection instance you can and should specify the type of object contained in the collection. 声明Collection实例时,可以并且应该指定集合中包含的对象的类型。Specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus reducing errors at runtime. 指定类型允许编译器(在编译时)验证放入集合的对象的类型是否正确,从而减少运行时的错误。For information on generic types, see the Generics (Updated) lesson.有关泛型类型的信息,请参阅泛型(已更新)课程。

When you understand how to use these interfaces, you will know most of what there is to know about the Java Collections Framework. 当您了解如何使用这些接口时,您将了解Java集合框架的大部分内容。This chapter discusses general guidelines for effective use of the interfaces, including when to use which interface. 本章讨论有效使用接口的一般准则,包括何时使用哪个接口。You'll also learn programming idioms for each interface to help you get the most out of it.您还将学习每个接口的编程习惯用法,以帮助您充分利用它。

To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. 为了保持核心集合接口的数量可管理,Java平台没有为每个集合类型的每个变体提供单独的接口。(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. 如果调用了不受支持的操作,则集合将抛出UnsupportedOperationExceptionImplementations 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平台的所有通用实现都支持所有可选操作。

The following list describes the core collection interfaces:以下列表描述了核心集合接口:

The last two core collection interfaces are merely sorted versions of Set and Map:最后两个核心集合接口仅为SetMap的排序版本:

To understand how the sorted interfaces maintain the order of their elements, see the Object Ordering section.要了解已排序的接口如何维护其元素的顺序,请参阅对象排序部分。


Previous page: Previous Lesson
Next page: The Collection Interface