Vector主要用来保存各种类型的对象(包括相同类型和不同类型的对象)。但是在一些情况下使用会给程序带来性能上的影响。这主要是由Vector类的两个特点所决定的。第一,Vector提供了线程的安全保护功能。即使Vector类中的许多方法同步。但是如果你已经确认你的应用程序是单线程,这些方法的同步就完全不必要了。第二,在Vector查找存储的各种对象时,常常要花很多的时间进行类型的匹配。而当这些对象都是同一类型时,这些匹配就完全不必要了。因此,有必要设计一个单线程的,保存特定类型对象的类或集合来替代Vector类.用来替换的程序如下(StringVector.java):
public class StringVector {
private String[] data;
private int count;
public StringVector() {
this(10); // default size is 10
}
public StringVector(int initialSize) {
data = new String[initialSize];
}
public void add(String str) {
// ignore null strings
if (str == null) {
return;
}
ensureCapacity(count + 1);
data[count++] = str;
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = data.length;
if (minCapacity > oldCapacity) {
String oldData[] = data;
int newCapacity = oldCapacity * 2;
data = new String[newCapacity];
System.arraycopy(oldData, 0, data, 0, count);
}
}
public void remove(String str) {
if (str == null) {
return; // ignore null str
}
for (int i = 0; i < count; i++) {
// check for a match
if (data[i].equals(str)) {
System.arraycopy(data, i + 1, data, i, count - 1); // copy data
// allow previously valid array element be gc'd
data[--count] = null;
return;
}
}
}
public final String getStringAt(int index) {
if (index < 0) {
return null;
} else if (index > count) {
return null; // index is > # strings
} else {
return data[index]; // index is good }
}
}
}
因此,代码:
Vector Strings=new Vector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=(String)Strings.elementAt(1);
可以用如下的代码替换:
StringVector Strings=new StringVector();
Strings.add(“One”);
Strings.add(“Two”);
String Second=Strings.getStringAt(1);
这样就可以通过优化线程来提高JAVA程序的性能。用于测试的程序如下(TestCollection.java):
import java.util.Vector;
public class TestCollection {
public static void main(String[] args) {
TestCollection collect = new TestCollection();
collect.main1("stringvector");
}
public void main1(String ...args) {
if (args.length == 0) {
System.out
.println("Usage: java TestCollection [ vector | stringvector ]");
System.exit(1);
}
if (args[0].equals("vector")) {
Vector store = new Vector();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
store.addElement("string");
}
long finish = System.currentTimeMillis();
System.out.println((finish - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String result = (String) store.elementAt(i);
}
finish = System.currentTimeMillis();
System.out.println((finish - start));
} else if (args[0].equals("stringvector")) {
StringVector store = new StringVector();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
store.add("string");
}
long finish = System.currentTimeMillis();
System.out.println((finish - start));
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String result = store.getStringAt(i);
}
finish = System.currentTimeMillis();
System.out.println((finish - start));
}
}
}
/* * * * * * * * * * * * * * * *TestCollection.java * * * * * * * * * * * * * * * * */
测试的结果如下(假设标准的时间为1,越小性能越好):