JAVA数据集合:Set与Get效率对比

1 实例测试代码

<pre name="code" class="java">        // 1 ArrayList set sort elements and list elements
	public static void printArrList() {
		List<String> listLined = new ArrayList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < 1000000; x++) {
			listLined.add(String.valueOf(x));
		} 
		for (int m = 0; m < listLined.size(); m++) {
			String tempString = listLined.get(m);
		}
		long st4 = System.currentTimeMillis();
		System.out.println("arrayList Get Elements time By normal =" + (st4 - st3));
	}

	// 2 LinedkList set sort elements and list elements
	public static void printLinkedList() {
		List<String> listLined = new LinkedList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < <span style="color:#FF0000;">100000</span>; x++) {
			listLined.add(String.valueOf(x));
		} 
		for (int m = 0; m < listLined.size(); m++) {
			String tempString = listLined.get(m);
		}
		long st4 = System.currentTimeMillis();
		System.out.println("LinkedList Get Elements time  By normal =" + (st4 - st3)); 
	}

	// 3 ArrayList set sort elements and list elements By FOR
	public static void printArrListByFor() {
		List<String> listLined = new ArrayList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < 1000000; x++) {
			listLined.add(String.valueOf(x));
		} 
		for (String m : listLined) {
			String tempString = m;
		}
		long st4 = System.currentTimeMillis();
		System.out.println("arrayList Get Elements By FOR  with time="
				+ (st4 - st3));
	}

	// 4 LinkedList set sort elements and list elements By FOR
	public static void printLinkedListByFor() {
		List<String> listLined = new LinkedList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < 1000000; x++) {
			listLined.add(String.valueOf(x));
		} 
		for (String m : listLined) {
			String tempString = m;
		}
		long st4 = System.currentTimeMillis();
		System.out.println("LinkedList Get Elements By FOR  with time="
				+ (st4 - st3));
	}

	// 5 ArrayList set sort elements and list elements By Itorator
	public static void printArrListByItorator() {
		List<String> listLined = new ArrayList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < 1000000; x++) {
			listLined.add(String.valueOf(x));
		} 
		Iterator<String> iterator = listLined.iterator();
		while( iterator.hasNext() ){ 
			String tempString = iterator.next();
		}
		long st4 = System.currentTimeMillis();
		System.out.println("arrayList Get Elements By Itorator  with time="
				+ (st4 - st3));
	}

	// 6 LinkedList set sort elements and list elements By Itorator
	public static void printLinkedListByItorator() {
		List<String> listLined = new LinkedList<String>(); 
		long st3 = System.currentTimeMillis();
		for (int x = 0; x < 1000000; x++) {
			listLined.add(String.valueOf(x));
		} 
		Iterator<String> iterator = listLined.iterator();
		while( iterator.hasNext() ){ 
			String tempString = iterator.next();
		}
		long st4 = System.currentTimeMillis();
		System.out.println("LinkedList Get Elements By Itorator  with time="
				+ (st4 - st3));
	}

	public static void main(String[] args) {
		for(int x = 1; x <= 5 ; x++ ){
                  System.out.println(" loop test time:" + x);
		printArrList();
			printLinkedList();
			printArrListByFor();
			printLinkedListByFor();
			printArrListByItorator();
			printLinkedListByItorator();
		}
	}


 


2   3次输出结果

<pre name="code" class="java">loop test time:1
arrayList Get Elements time By normal =184
LinkedList Get Elements time  By normal =7434
arrayList Get Elements By FOR  with time=218
LinkedList Get Elements By FOR  with time=138
arrayList Get Elements By Itorator  with time=231
LinkedList Get Elements By Itorator  with time=92

 loop test time:2
arrayList Get Elements time By normal =98
LinkedList Get Elements time  By normal =7428
arrayList Get Elements By FOR  with time=185
LinkedList Get Elements By FOR  with time=62
arrayList Get Elements By Itorator  with time=92
LinkedList Get Elements By Itorator  with time=180

 loop test time:3
arrayList Get Elements time By normal =43
LinkedList Get Elements time  By normal =8004
arrayList Get Elements By FOR  with time=104
LinkedList Get Elements By FOR  with time=64
arrayList Get Elements By Itorator  with time=76
LinkedList Get Elements By Itorator  with time=51

 loop test time:4
arrayList Get Elements time By normal =47
LinkedList Get Elements time  By normal =7490
arrayList Get Elements By FOR  with time=109
LinkedList Get Elements By FOR  with time=64
arrayList Get Elements By Itorator  with time=99
LinkedList Get Elements By Itorator  with time=65
 loop test time:5
arrayList Get Elements time By normal =62
LinkedList Get Elements time  By normal =7890
arrayList Get Elements By FOR  with time=44
LinkedList Get Elements By FOR  with time=57
arrayList Get Elements By Itorator  with time=84
LinkedList Get Elements By Itorator  with time=49

 
注意:1  printArrList(); printArrListByFor(); printLinkedListByFor();printArrListByItorator();处于千万量级别, 

             2 printLinkedList()方法只能处于十万级别量, printLinkedListByItorator()只能处于百万量级别,千万量级别会报:java.lang.OutOfMemoryError: Java heap space

3 结论

1. ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。

 2. 对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。

 3. 对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据 ,但在尾部新增和删除除外。

4. For(foreach)、Iterator原理都是基于迭代器,区别不大,在获取元素的时候的时间复杂度是 O(1) (使用了 getNext()hasNext() 方法),最终的时间复杂度为 O(n), 并且两者有迭代器的内存开销;
    而LinkedList循环里每次在调用 get(i) 的时候花费的时间复杂度为 O(n),最终整个循环的时间复杂度就是 O(n^2);
        ArrayList循环里每次在调用  get(i) 方法的时间复杂度就是 O(1),最终整个循环的时间复杂度就是 O(n);


4 建议

  如果不考虑内存空间:使用普通的ArrayList 的Iterator 方法遍历,为最优方案

  考虑内存空间:使用普通的ArrayList,并for遍历为最优方案


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值