java 四种遍历List的方法及比较
java 四种遍历List的方法及比较
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();)
相关文档:
/************Student.java begin***************/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Student {
private String name;
private String password;
public String getName() {
return name;
}
public ......
转自: http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=4465
Cookie在Web应用程序中被广泛采用,维护浏览器和服务器之间的状态。遗憾的是这一特性在Java ME平台中并没有得到支持。因此,要想维持客户端和服务器端的状态则必须使用URL重写的方式。URL重写操作起来比较麻烦,所以研究一下cookie的原理并在 ......
原文:Some Java Concurrency Tips
作者:Carol McDonald
出处:
http://weblogs.java.net/blog/caroljmcdonald/archive/2009/09/17/some-java-concurrency-tips
这是来自Joshua Bloch、Brian Goetz和其他人的一个关于一些并发技巧的汇总。
首先选择不可变的对象/数据
不可变对象(immutable ......
JDK1.4中
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
JDK1.5中,应用新特性For-Each循环
Map m = new HashMap();
......