package cn.jellen.system.action;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* List 遍历方法及比较
* @author KH
* 有时候复制、粘贴出现的错误:Syntax error, insert ";" to complete ReturnStatement 删除多余的空格,重新格式化一下就OK喽!
*/
public class Test {
public static void main(String args[]){
List<Long> lists = new ArrayList<Long>();
for(Long i=0L;i<888888;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);
/* 参考结果
One:13625
Two:10797
Three:10594
four:10750
*/
}
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;
}
}
List 遍历方法及比较
最新推荐文章于 2021-04-21 15:13:57 发布