首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java List遍历步骤 及其效率对比

2012-12-28 
Java List遍历方法 及其效率对比One:14109Two:14000Three:15141four:14297?package com.zbalpha.testimpo

Java List遍历方法 及其效率对比

One:14109
Two:14000
Three:15141
four:14297

?

package com.zbalpha.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListTest {
??? public static void main(String args[]){
??????? List<Long> lists = new ArrayList<Long>();

??????? for(Long i=0l;i<1000000l;i++){
??????????? lists.add(i);
??????? }?
????????
??????? Long oneOk = oneMethod(lists);
??????? Long twoOk = twoMethod(lists);
??????? Long threeOk = threeMethod(lists);
??????? Long fourOk = fourMethod(lists);
????????
??????? System.out.println("One:" + oneOk);
??????? System.out.println("Two:" + twoOk);
??????? System.out.println("Three:" + threeOk);
??????? System.out.println("four:" + fourOk);
????????
??? }
????
??? public static Long oneMethod(List<Long> lists){
??????? Long timeStart = System.currentTimeMillis();
??????? for(int i=0;i<lists.size();i++)??? {
??????????? System.out.println(lists.get(i));
??????? }
??????? Long timeStop = System.currentTimeMillis();

??????? return timeStop -timeStart ;
??? }
????
??? public static Long twoMethod(List<Long> lists){
??????? Long timeStart = System.currentTimeMillis();
??????? for(Long string : lists)??? {
??????????? System.out.println(string);
??????? }
??????? Long timeStop = System.currentTimeMillis();

??????? return timeStop -timeStart ;
??? }
????
??? public static Long threeMethod(List<Long> lists){
????????
??????? Long timeStart = System.currentTimeMillis();
??????? Iterator<Long> it = lists.iterator();
??????? while (it.hasNext())
??????? {
??????????????? System.out.println(it.next());
??????? }
??????? Long timeStop = System.currentTimeMillis();

??????? return timeStop -timeStart ;
??? }????
????
????????
????
??? public static Long fourMethod(List<Long> lists){
????????
??????? Long timeStart = System.currentTimeMillis();
??????? for(Iterator<Long> i = lists.iterator(); i.hasNext();)??? {
??????????? System.out.println(i.next());
??????? }
??????? Long timeStop = System.currentTimeMillis();

??????? return timeStop -timeStart ;
??? }????
}

来自:?http://hi.baidu.com/btb368/blog/item/f7aa502d67e119eb8a1399a4.html

热点排行