第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
例:
HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:
public class HashMapTest {
public static void main(String[] args) ...{
HashMap hashmap = new HashMap();
for (int i = 0; i < 1000; i ) ...{
hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) ...{
System.out.print(hashmap.get(iterator.next()));
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}
public static void listHashMap() ...{
java.util.HashMap hashmap = new java.util.HashMap();
for (int i = 0; i < 1000; i ) ...{
hashmap.put("" i, "thanks");
}
long bs = Calendar.getInstance().getTimeInMillis();
java.util.Iterator it = hashmap.entrySet().iterator();
while (it.hasNext()) ...{
java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.print(entry.getValue());
}
System.out.println();
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}
}
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。
http://ludaojuan21.iteye.com/blog/243475
分享到:
相关推荐
NULL 博文链接:https://jsjxqjy.iteye.com/blog/2072705
7_4.有集合A={1,2,3,4}和B={1,3,7,9,11},编写一个应用程序输出A和B交集、并集和差集...编写一个应用程序,要求将LinkedList<E>创建的对象写入到文件,然后读出一个LinkedList<E>对象,并遍历LinkedList<E>节点中的数据
Map里存放的是List时<br><br><logic:iterate id="destMap" name="srcMap"><br><br> <logic:iterate id="bean" name="destMap" property="value" /><br><br> <bean:write name="bean" property="name" /><br><br> </...
Freemarker中遍历list集合实例
Java中 List《map》把里面map中某个key相同记录的其他key 的 value 合并 Java中 List《map》把里面map中某个key相同记录的其他key 的 value 合并
Map<String,String> map = new HashMap(); map.forEach((x,y) -> System.out.println(x+":"+y)); 集合里放对象 List<Book> books; books.forEach(x -> System.out.println(x.getBookName())); List...
NULL 博文链接:https://takeme.iteye.com/blog/1663039
<br> 辨析List,Set和Map接口。<br>• 理解List接口,辨别使用List接口的实现类。<br>• 理解Set接口,辨别使用Set接口的实现类。<br>• 理解Map接口,辨别使用Map接口的实现类。<br> 培养面向接口编程的思维...
private List<Menu> getChildrens(Menu root, List<Menu> all) { List<Menu> children = all.stream().filter(m -> { return Objects.equals(m.getParentId(), root.getId()); }).map((m) -> { m.setChildList...
Map集合的四种遍历方式
此java类实现了对数据表的分类递归树的实现,为本人倾力之作,后期,会发布js版,敬请期待!
STL容器部分主要由头文件<vector>、<list>、<deque>、<set>、< map>、<stack>和<queue>组成。 (2)算法(Algorithms)。包括各种基本算法,如比较、交换、查找、排序、遍历操作、复制、修改、移除、反转、合并...
测试vector, list, map遍历时删除符合条件的元素 本代码测试环境: ubuntu12 + win7_x64 */ #include <iostream> #include <vector> #include <list> #include <map> #include <iterator> using namespace std; ...
#include <map> #include <string> #include <list> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { printf(“run main”); vector<int> vect ; vect.push_back(1); vect.push_back(2); vect.push_ba
C++ STL list 遍历删除崩溃 ...#include <map> #include <list> using namespace std; typedef std::list<int> TESTLIST; int _tmain(int argc, _TCHAR* argv[]) { TESTLIST t; for (int i = 0; i < 10;i++) {
NULL 博文链接:https://takeme.iteye.com/blog/1663239
相信大家在日常的开发过程中,经常需要对List或Map里面的符合某种业务的数据进行删除,但是如果不了解里面的...下面这篇文章将会给大家详细介绍Java如何在List和Map遍历过程中删除元素,有需要的朋友们可以参考借鉴。
主要介绍了java 遍历Map及Map转化为二维数组的实例的相关资料,希望通过本文能帮助到大家,实现这样的功能,需要的朋友可以参考下
Map<Integer,String> map=new TreeMap<Integer,String>(); //Map map=new HashMap(); map.put(1, "yi"); map.put(23, "er"); map.put(12, "san"); map.put(3, "si"); //遍历map Set keys=map.keySet(); ...
jquery获得单选框和下拉框的值,java日期处理,遍历list,遍历map