コレクションテスト

3041 ワード

public class ListTest {
	long startTime;
	long endTime;
	public void testArrayList(){
		List<String> list = new ArrayList<String>();
		list.add("zhangsan");
		list.add("lisi");
		list.add("admin");
		list.add("custom");
		
		startTime =  System.nanoTime();
		System.out.println("*************ArrayList *************");
		list.add(1, "insert");
		endTime =  System.nanoTime();
		System.out.println("*** , "+(endTime - startTime)+"ns****");
		
		System.out.println("************ FOR *************");
		for(String item : list){
			System.out.println(item);
		}
		
		System.out.println("************ListIterator*************");
		ListIterator<String> iter = list.listIterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}
		
		System.out.println("************Iterator*************");
		Iterator<String> it = list.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
	
	public void testLinkList(){
		List<String> lt = new LinkedList<String>();
		lt.add("LinkedList1");
		lt.add("LinkedList3");
		lt.add("LinkedList2");
		lt.add("LinkedList4");
		startTime =  System.nanoTime();
		System.out.println("*************LinkedList *************");
		lt.add(1, "insert");
		endTime =  System.nanoTime();
		System.out.println("*** , "+(endTime - startTime)+"ns****");
		
		System.out.println("************ FOR *************");
		for(String item : lt){
			System.out.println(item);
		}
	
	}
	
	public void testVector(){
		Vector<String> v = new Vector<String>();
		v.add("Vector1");
		v.add("Vector4");
		v.add("Vector2");
		v.add("Vector3");
		v.add("Vector6");
		v.add("Vector5");
		
		System.out.println("************Vector FOR *************");
		for(String item : v){
			System.out.println(item);
		}
	}
	
	public void testSet(){
		Set<String> set = new HashSet<String>();
		set.add("Set1");
		set.add("Set2");
		set.add("Set3");
		set.add("Set2");
		set.add("Set4");
		set.add("Set5");
		
		System.out.println("************HashSet FOR *************");
		for(String item : set){
			System.out.println(item);
		}
	}
	
	public static void main(String[] args) {
		ListTest list = new ListTest();
		list.testArrayList();
		list.testLinkList();
		list.testVector();
		list.testSet();
	}
}