正确使用Vector类

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,越小性能越好):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值