The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Many programmers will never need to implement their own 许多程序员永远不需要实现自己的Collection
s classes. Collection
类。You can go pretty far using the implementations described in the preceding sections of this chapter. 使用本章前面几节中描述的实现,您可以走得更远。However, someday you might want to write your own implementation. 然而,有一天您可能会想编写自己的实现。It is fairly easy to do this with the aid of the abstract implementations provided by the Java platform. 借助Java平台提供的抽象实现,实现这一点相当容易。Before we discuss how to write an implementation, let's discuss why you might want to write one.在讨论如何编写实现之前,让我们先讨论一下为什么要编写一个实现。
The following list illustrates the sort of custom 下面的列表说明了您可能想要实现的自定义Collection
s you might want to implement. Collection
的种类。It is not intended to be exhaustive:其目的并非详尽无遗:
Collection
implementations reside in main memory and vanish when the program exits. Collection
实现都驻留在主内存中,并在程序退出时消失。Map
containing real-time telemetry data. Map
。get
operation.get
操作。List
containing long runs of identical element values. List
。ArrayList
.ArrayList
需要更少的空间,但需要更多的时间。Collection
that offers constant-time containment checks while allowing duplicate elements. HashMap
.HashMap
上实现这样一个集合相当简单。List
instances representing a contiguous range of Integer
s.Integer
范围的List
实例。Writing a custom implementation is surprisingly easy. 编写自定义实现非常简单。The Java Collections Framework provides abstract implementations designed expressly to facilitate custom implementations. Java 集合框架提供了抽象实现,这些抽象实现是专门为方便定制实现而设计的。We'll start with the following example of an implementation of 我们将从以下Arrays.asList
.Arrays.asList
实现的示例开始。
public static <T> List<T> asList(T[] a) { return new MyArrayList<T>(a); } private static class MyArrayList<T> extends AbstractList<T> { private final T[] a; MyArrayList(T[] array) { a = array; } public T get(int index) { return a[index]; } public T set(int index, T element) { T oldValue = a[index]; a[index] = element; return oldValue; } public int size() { return a.length; } }
Believe it or not, this is very close to the implementation that is contained in 信不信由你,这与java.util.Arrays
. java.util.Arrays
中包含的实现非常接近。It's that simple! 就这么简单!You provide a constructor and the 您提供了一个构造函数以及get
, set
, and size
methods, and AbstractList
does all the rest. get
、set
和size
方法,而AbstractList
完成了所有其余的工作。You get the 您可以免费获得ListIterator
, bulk operations, search operations, hash code computation, comparison, and string representation for free.ListIterator
、批量操作、搜索操作、哈希代码计算、比较和字符串表示。
Suppose you want to make the implementation a bit faster. 假设您想让实现更快一点。The API documentation for abstract implementations describes precisely how each method is implemented, so you'll know which methods to override to get the performance you want. 抽象实现的API文档精确地描述了每个方法的实现方式,因此您将知道要覆盖哪些方法以获得所需的性能。The preceding implementation's performance is fine, but it can be improved a bit. 前面的实现的性能很好,但可以稍微改进一下。In particular, the 特别是,toArray
method iterates over the List
, copying one element at a time. toArray
方法在List
上迭代,一次复制一个元素。Given the internal representation, it's a lot faster and more sensible just to clone the array.考虑到内部表示,仅克隆阵列就更快、更合理。
public Object[] toArray() { return (Object[]) a.clone(); }
With the addition of this override and a few more like it, this implementation is exactly the one found in 加上这个覆盖和其他一些类似的功能,这个实现就是java.util.Arrays
. java.util.Arrays
中的实现。In the interest of full disclosure, it's a bit tougher to use the other abstract implementations because you will have to write your own iterator, but it's still not that difficult.为了充分公开,使用其他抽象实现有点困难,因为您必须编写自己的迭代器,但仍然没有那么困难。
The following list summarizes the abstract implementations:以下列表总结了抽象实现:
AbstractCollection
a Collection
that is neither a Set
nor a List
. At a minimum, you must provide the iterator
and the size
methods.AbstractSet
a Set
; use is identical to AbstractCollection
.AbstractList
a List
backed up by a random-access data store, such as an array. At a minimum, you must provide the positional access
methods (get
and, optionally, set
, remove
, and add
) and the size
method. The abstract class takes care of listIterator
(and iterator
).AbstractSequentialList
a List
backed up by a sequential-access data store, such as a linked list. At a minimum, you must provide the listIterator
and size
methods. The abstract class takes care of the positional access methods. (This is the opposite of AbstractList
.)AbstractQueue
at a minimum, you must provide the offer
, peek
, poll
, and size
methods and an iterator
supporting remove
.AbstractMap
a Map
. At a minimum you must provide the entrySet
view. This is typically implemented with the AbstractSet
class. If the Map
is modifiable, you must also provide the put
method.The process of writing a custom implementation follows:编写自定义实现的过程如下所示: