Collections类中的实用东西
Collections类中含有其他大量有用的实用东西(如表二):
enumeration(Collection) 为自变量发生原始气势气魄的Enumeration(列举)
max(Collection),min(Collection) 在自变量顶用荟萃内工具的自然较量要领发生最大或最小元素
max(Collection,Comparator),min(Collection,Comparator) 在荟萃内用较量器发生最大或最小元素
nCopies(int n, Object o) 返回长度为n的一个不行变列表,它的所有句柄均指向o
subList(List,int min,int max) 返回由指定参数列表后推获得的一个新列表。可将这个列表想象成一个“窗口”,它自索引为min的处所开始,正好竣事于max的前面
|
enumeration(Collection) |
Produces an old-style Enumeration for the argument. |
|
max(Collection) min(Collection) |
Produces the maximum or minimum element in the argument using the natural comparison method of the objects in the Collection. |
|
max(Collection, Comparator) min(Collection, Comparator) |
Produces the maximum or minimum element in the Collection using the Comparator. |
|
nCopies(int n, Object o) |
Returns an immutable List of size n whose handles all point to o. |
|
subList(List, int min, int max) |
Returns a new List backed by the specified argument List that is a window into that argument with indexes starting at min and stopping just before max. |
表二
留意min()和max()都是伴同Collection工具事情的,而非伴同List,所以不必担忧Collection是否需要排序(就象早先指出的那样,在执行一次binarySearch()——即二进制搜索——之前,必需对一个List可能一个数组执行sort())。
1. 使Collection或Map不行修改
凡是,建设Collection或Map的一个“只读”版本显得更有利一些。Collections类答允我们到达这个方针,要领是将原始容器通报进入一个要领,并令其传回一个只读版本。这个要领共有四种变革形式,别离用于Collection(假如不想把荟萃看成一种更非凡的范例看待)、List、Set以及Map。下面这个例子演示了为它们别离构建只读版本的正确要领:
//: ReadOnly.java
// Using the Collections.unmodifiable methods
package c08.newcollections;
import java.util.*;
public class ReadOnly {
public static void main(String[] args) {
Collection c = new ArrayList();
Collection1.fill(c); // Insert useful data
c = Collections.unmodifiableCollection(c);
Collection1.print(c); // Reading is OK
//! c.add("one"); // Can't change it
List a = new ArrayList();
Collection1.fill(a);
a = Collections.unmodifiableList(a);
ListIterator lit = a.listIterator();
System.out.println(lit.next()); // Reading OK
//! lit.add("one"); // Can't change it
Set s = new HashSet();
Collection1.fill(s);
s = Collections.unmodifiableSet(s);
Collection1.print(s); // Reading OK
//! s.add("one"); // Can't change it
Map m = new HashMap();
Map1.fill(m, Map1.testData1);
m = Collections.unmodifiableMap(m);
Map1.print(m); // Reading OK
//! m.put("Ralph", "Howdy!");
}
} ///:~
对付每种环境,在将其正式变为只读以前,都必需用有有效的数据填充容器。一旦载入乐成,最佳的做法就是用“不行修改”挪用发生的句柄替换现有的句柄。这样做可有效制止将其酿成不行修改后不慎改变个中的内容。在另一方面,该东西也答允我们在一个类中将可以或许修改的容器保持为private状态,并可从一个要领挪用中返回指向谁人容器的一个只读句柄。这样一来,固然我们可在类里修改它,但其他任何人都只能读。
为特定范例挪用“不行修改”的要领不会造成编译期间的查抄,但一旦产生任何变革,对修改特定容器的要领的挪用便会发生一个UnsupportedOperationException违例。
2. Collection或Map的同步
synchronized要害字是“多线程”机制一个很是重要的部门。我们到第14章才会对这一机建造深入的探讨。在这儿,各人只需留意到Collections类提供了对整个容器举办自动同步的一种途径。它的语法与“不行修改”的要领是雷同的:
//: Synchronization.java
// Using the Collections.synchronized methods
package c08.newcollections;
import java.util.*;
public class Synchronization {
public static void main(String[] args) {
Collection c =
Collections.synchronizedCollection(
new ArrayList());
List list = Collections.synchronizedList(
new ArrayList());
Set s = Collections.synchronizedSet(
new HashSet());
Map m = Collections.synchronizedMap(
new HashMap());
}
} ///:~
#p#分页标题#e#
在这种环境下,我们通过适当的“同步”要领直接通报新容器;这样做可制止不慎袒暴露未同步的版本。
新荟萃也提供了能防备多个历程同时修改一个容器内容的机制。若在一个容器里重复,同时另一些历程参与,并在谁人容器中插入、删除或修改一个工具,便谋面对产生斗嘴的危险。我们大概已通报了谁人工具,大概它位位于我们前面,大概容器的巨细在我们挪用size()后已产生了收缩——我们面对各类百般大概的危险。针对这个问题,新的荟萃库集成了一套办理的机制,能查出除我们的历程本身需要认真的之外的、对容器的其他任何修改。若探测到有其他方面也筹备修改容器,便会当即发生一个ConcurrentModificationException(并发修改违例)。我们将这一机制称为“当即失败”——它并不消更巨大的算法在“今后”侦测问题,而是“当即”发生违例。