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发行说明。
This section describes several mini-implementations that can be more convenient and more efficient than general-purpose implementations when you don't need their full power. 本节介绍了几种小型实现,当您不需要它们的全部功能时,它们比通用实现更方便、更高效。All the implementations in this section are made available via static factory methods rather than 本节中的所有实现都是通过静态工厂方法而不是public
classes.public
类提供的。
The Arrays.asList
method returns a List
view of its array argument. Changes to the List
write through to the array and vice versa. The size of the collection is that of the array and cannot be changed. If the add
or the remove
method is called on the List
, an UnsupportedOperationException
will result.
The normal use of this implementation is as a bridge between array-based and collection-based APIs. It allows you to pass an array to a method expecting a Collection
or a List
. However, this implementation also has another use. If you need a fixed-size List
, it's more efficient than any general-purpose List
implementation. This is the idiom.
List<String> list = Arrays.asList(new String[size]);
Note that a reference to the backing array is not retained.请注意,不会保留对备份阵列的引用。
Occasionally you'll need an immutable List
consisting of multiple copies of the same element. The Collections.nCopies
method returns such a list. This implementation has two main uses. The first is to initialize a newly created List
; for example, suppose you want an ArrayList
initially consisting of 1,000 null
elements. The following incantation does the trick.
List<Type> list = new ArrayList<Type>(Collections.nCopies(1000, (Type)null));
Of course, the initial value of each element need not be null
. The second main use is to grow an existing List
. For example, suppose you want to add 69 copies of the string "fruit bat"
to the end of a List<String>
. It's not clear why you'd want to do such a thing, but let's just suppose you did. The following is how you'd do it.
lovablePets.addAll(Collections.nCopies(69, "fruit bat"));
By using the form of addAll
that takes both an index and a Collection
, you can add the new elements to the middle of a List
instead of to the end of it.
Sometimes you'll need an immutable singleton Set
, which consists of a single, specified element. The Collections.singleton
method returns such a Set
. One use of this implementation is to remove all occurrences of a specified element from a Collection
.
c.removeAll(Collections.singleton(e));
A related idiom removes all elements that map to a specified value from a Map
. For example, suppose you have a Map
job
that maps people to their line of work and suppose you want to eliminate all the lawyers. The following one-liner will do the deed.
job.values().removeAll(Collections.singleton(LAWYER));
One more use of this implementation is to provide a single input value to a method that is written to accept a collection of values.这个实现的另一个用途是为一个方法提供一个输入值,该方法被编写为接受一组值。
The Collections
class provides methods to return the empty Set
, List
, and Map
emptySet
, emptyList
, and emptyMap
. The main use of these constants is as input to methods that take a Collection
of values when you don't want to provide any values at all, as in this example.
tourist.declarePurchases(Collections.emptySet());