Skip to Content
Java 泛型与集合,第二版
book

Java 泛型与集合,第二版

by Maurice Naftalin, Philip Wadler
June 2025
Intermediate to advanced
368 pages
4h 53m
Chinese
O'Reilly Media, Inc.
Content preview from Java 泛型与集合,第二版

第 11 章 序列集合接口 序列集合接口

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

接口SequencedCollection 在集合框架的设计空间中占有独特的地位。该接口并没有指定特定抽象数据结构的契约,而是统一了各种已有类型(List 、NavigableSet 、Deque 和LinkedHashSet)所暴露的行为,这些类型分布在类型层次结构中,而且大多没有其他关联(见图 11-1)。

jgc2 1101
图 11-1. 用于SequencedCollection

这些集合的共同点是,它们都表示一个元素序列,其顺序在语义上是重要的(它们服从一个总顺序,在 JDK 文档中称为相遇顺序),无论该顺序是内部还是外部强加的(请参阅"有序集合")。

序列收集的方法

正是Deque (参见"Deque")引入了一系列方法,这些方法现在已被推广到所有具有相遇顺序的集合,提供了添加、检查和移除序列中第一个和最后一个元素的功能。这些功能中的某些功能以前在这些集合中以不同的名称提供过:例如,NavigableSet 支持移除第一个和最后一个元素,但不支持检查它们,而List 只直接支持添加最后一个元素,所有其他操作都需要指定序列位置。表 12-1列出了NavigableSet 方法调用与SequencedCollection 对应方法的对应关系,表 14-1 列出了List 的对应关系。Deque 不需要这样的表格,因为除了reversed 之外,SequencedCollection 的操作都是从Deque 升级而来的。

当然,如果没有一些特殊情况,要统一这么多不同集合的行为是不可能的。例如,在NavigableSet 中添加第一个或最后一个元素是毫无意义的,因为这些集合中所有元素的位置都是由内部排序算法决定的。再比如,不可修改列表不支持结构修改,如添加和删除元素。这些特殊情况将在涉及不同集合类型的章节中讨论。

SequencedCollection 提供的唯一新方法是reversed ,它提供了原始集合的反向排序视图(参见"视图")。这使得任何有相遇顺序的集合的用户都可以反向处理有序集合(以前只有NavigableSet 的客户端可以通过其descendingSet 方法使用)。

添加元素

void addFirst(E e)
将e 添加为该集合的第一个元素
void addLast(E e)
将e 添加为本集合的最后一个元素

这些方法将元素添加到SequencedCollection 的两端。这些方法的合约规定,在成功执行这些方法后,集合必须包含所提供的元素,并分别作为相遇顺序中的第一个或最后一个元素。

检查元件

E getFirst()
返回此集合的第一个元素
E getLast()
返回该集合的最后一个元素

这些方法检查SequencedCollection 两端的元素。在空集合上调用检查方法将产生NoSuchElement​Excep⁠tion 。

移除元素

E removeFirst()
移除并返回该集合的第一个元素
E removeLast()
移除并返回此集合的最后一个元素

这些方法可从SequencedCollection 的两端移除

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java Server Pages

Java Server Pages

Hans Bergsten
JUnit in Action

JUnit in Action

Ted Husted, Vincent Massol

Publisher Resources

ISBN: 9798341661035