概述
2019独角兽企业重金招聘Python工程师标准>>>
package cn.jiguang.base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
public class CollectionTest {
public void JDK8DataTimeClockTest() {
}
@Test
public void foreachQuery() {
int count = 0;
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100000; i++) {
map.put("key=" + i, "value=" + i);
}
long startTimeFor = System.currentTimeMillis();
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
count++;
}
System.out.println("查询记录数=" + count);
System.out.println("增强for查询耗时:" + (startTimeFor - System.currentTimeMillis()));
}
@Test
public void iteratorQuery() {
int count = 0;
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 1000000; i++) {
map.put("key=" + i, "value=" + i);
}
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
long startTimeIterator = System.currentTimeMillis();
while (iterator.hasNext()) {
String key = iterator.next().getKey();
String value = iterator.next().getValue();
System.out.println(key);
System.out.println(value);
count++;
}
System.out.println("查询记录数=" + count);
System.out.println("迭代器Iterator查询耗时:" + (startTimeIterator - System.currentTimeMillis()));
}
}
转载于:https://my.oschina.net/u/3744350/blog/1615222
最后
以上就是复杂诺言为你收集整理的Iterator迭代器和foreach增强for循环的效率比较的全部内容,希望文章能够帮你解决Iterator迭代器和foreach增强for循环的效率比较所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复