从早些时候的那幅示意图可以看出,实际上只有三个集合组件:map,list和set。而且每个接口只有两种或三种实施方案。若需使用由一个特定的接口提供的功能,如何才能决定到底采取哪一种方案呢?
为理解这个问题,必须认识到每种不同的实施方案都有自己的特点、优点和缺点。比如在那张示意图中,可以看到hashtable,vector和stack的“特点”是它们都属于“传统”类,所以不会干扰原有的代码。但在另一方面,应尽量避免为新的(java 1.2)代码使用它们。
其他集合间的差异通常都可归纳为它们具体是由什么“后推”的。换言之,取决于物理意义上用于实施目标接口的数据结构是什么。例如,arraylist,linkedlist以及vector(大致等价于arraylist)都实现了list接口,所以无论选用哪一个,我们的程序都会得到类似的结果。然而,arraylist(以及vector)是由一个数组后推得到的;而linkedlist是根据常规的双重链接列表方式实现的,因为每个单独的对象都包含了数据以及指向列表内前后元素的句柄。正是由于这个原因,假如想在一个列表中部进行大量插入和删除操作,那么linkedlist无疑是最恰当的选择(linkedlist还有一些额外的功能,建立于abstractsequentiallist中)。若非如此,就情愿选择arraylist,它的速度可能要快一些。
作为另一个例子,set既可作为一个arrayset实现,亦可作为hashset实现。arrayset是由一个arraylist后推得到的,设计成只支持少量元素,特别适合要求创建和删除大量set对象的场合使用。然而,一旦需要在自己的set中容纳大量元素,arrayset的性能就会大打折扣。写一个需要set的程序时,应默认选择hashset。而且只有在某些特殊情况下(对性能的提升有迫切的需求),才应切换到arrayset。
1. 决定使用何种list
为体会各种list实施方案间的差异,最简便的方法就是进行一次性能测验。下述代码的作用是建立一个内部基础类,将其作为一个测试床使用。然后为每次测验都创建一个匿名内部类。每个这样的内部类都由一个test()方法调用。利用这种方法,可以方便添加和删除测试项目。
//: listperformance.java
// demonstrates performance differences in lists
package c08.newcollections;
import java.util.*;
public class listperformance {
private static final int reps = 100;
private abstract static class tester {
string name;
int size; // test quantity
tester(string name, int size) {
this.name = name;
this.size = size;
}
abstract void test(list a);
}
private static tester[] tests = {
new tester("get", 300) {
void test(list a) {
for(int i = 0; i < reps; i++) {
for(int j = 0; j < a.size(); j++)
a.get(j);
}
}
},
new tester("iteration", 300) {
void test(list a) {
for(int i = 0; i < reps; i++) {
iterator it = a.iterator();
while(it.hasnext())
it.next();
}
}
},
new tester("insert", 1000) {
void test(list a) {
int half = a.size()/2;
string s = "test";
listiterator it = a.listiterator(half);
for(int i = 0; i < size * 10; i++)
it.add(s);
}
},
new tester("remove", 5000) {
void test(list a) {
listiterator it = a.listiterator(3);
while(it.hasnext()) {
it.next();
it.remove();
}
}
},
};
public static void test(list a) {
// a trick to print out the class name:
system.out.println("testing " +
a.getclass().getname());
for(int i = 0; i < tests.length; i++) {
collection1.fill(a, tests[i].size);
system.out.print(tests[i].name);
long t1 = system.currenttimemillis();
tests[i].test(a);
long t2 = system.currenttimemillis();
system.out.println(": " + (t2 - t1));
}
}
public static void main(string[] args) {
test(new arraylist());
test(new linkedlist());
}
} ///:~
内部类tester是一个抽象类,用于为特定的测试提供一个基础类。它包含了一个要在测试开始时打印的字串、一个用于计算测试次数或元素数量的size参数、用于初始化字段的一个构建器以及一个抽象方法test()。test()做的是最实际的测试工作。各种类型的测试都集中到一个地方:tests数组。我们用继承于tester的不同匿名内部类来初始化该数组。为添加或删除一个测试项目,只需在数组里简单地添加或移去一个内部类定义即可,其他所有工作都是自动进行的。
首先用元素填充传递给test()的list,然后对tests数组中的测试计时。由于测试用机器的不同,结果当然也会有所区别。这个程序的宗旨是揭示出不同集合类型的相对性能比较。下面是某一次运行得到的结果:
类型 获取 反复 插入 删除
arraylist 110 270 1920 4780
linkedlist 1870 7580 170 110
可以看出,在arraylist中进行随机访问(即get())以及循环反复是最划得来的;但对于linkedlist却是一个不小的开销。但另一方面,在列表中部进行插入和删除操作对于linkedlist来说却比arraylist划算得多。我们最好的做法也许是先选择一个arraylist作为自己的默认起点。以后若发现由于大量的插入和删除造成了性能的降低,再考虑换成linkedlist不迟。
2. 决定使用何种set
可在arrayset以及hashset间作出选择,具体取决于set的大小(如果需要从一个set中获得一个顺序列表,请用treeset;注释⑧)。下面这个测试程序将有助于大家作出这方面的抉择:
//: setperformance.java
package c08.newcollections;
import java.util.*;
public class setperformance {
private static final int reps = 200;
private abstract static class tester {
string name;
tester(string name) { this.name = name; }
abstract void test(set s, int size);
}
private static tester[] tests = {
new tester("add") {
void test(set s, int size) {
for(int i = 0; i < reps; i++) {
s.clear();
collection1.fill(s, size);
}
}
},
new tester("contains") {
void test(set s, int size) {
for(int i = 0; i < reps; i++)
for(int j = 0; j < size; j++)
s.contains(integer.tostring(j));
}
},
new tester("iteration") {
void test(set s, int size) {
for(int i = 0; i < reps * 10; i++) {
iterator it = s.iterator();
while(it.hasnext())
it.next();
}
}
},
};
public static void test(set s, int size) {
// a trick to print out the class name:
system.out.println("testing " +
s.getclass().getname() + " size " + size);
collection1.fill(s, size);
for(int i = 0; i < tests.length; i++) {
system.out.print(tests[i].name);
long t1 = system.currenttimemillis();
tests[i].test(s, size);
long t2 = system.currenttimemillis();
system.out.println(": " +
((double)(t2 - t1)/(double)size));
}
}
public static void main(string[] args) {
// small:
test(new treeset(), 10);
test(new hashset(), 10);
// medium:
test(new treeset(), 100);
test(new hashset(), 100);
// large:
test(new hashset(), 1000);
test(new treeset(), 1000);
}
} ///:~
⑧:treeset在本书写作时尚未成为一个正式的特性,但在这个例子中可以很轻松地为其添加一个测试。
最后对arrayset的测试只有500个元素,而不是1000个,因为它太慢了。
类型 测试大小 添加 包含 反复

进行add()以及contains()操作时,hashset显然要比arrayset出色得多,而且性能明显与元素的多寡关系不大。一般编写程序的时候,几乎永远用不着使用arrayset。
闽公网安备 35060202000074号