4 种 HashMap 中对象遍历的方法

红薯 发布于 2012/03/16 11:11
阅读 1K+
收藏 14

使用 Java 5 的 foreach 进行迭代和循环

HashMap<String, String> loans = new HashMap<String, String>();
loans.put("name", "oschina");
loans.put("url", "http://www.oschina.net/");

for (String key : loans.keySet()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating or looping map using java5 foreach loop");
   System.out.println("key: " + key + " value: " + loans.get(key));
}
输出结果
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: name value: oschina
------------------------------------------------
Iterating or looping map using java5 foreach looop

key: url value: http://www.oschina.net/

使用 KeySet 迭代器

Set<String> keySet = loans.keySet();
Iterator<String> keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating Map in Java using KeySet Iterator");
   String key = keySetIterator.next();
   System.out.println("key: " + key + " value: " + loans.get(key));
}

输出结果:

------------------------------------------------
Iterating Map in Java using KeySet Iterator
key: home loan value: citibank
------------------------------------------------
Iterating Map in Java using KeySet Iterator

key: personal loan value: Wells Fargo

使用 EntrySet 进行迭代

Set<Map.Entry<String, String>> entrySet = loans.entrySet();
for (Entry entry : entrySet) {
   System.out.println("------------------------------------------------");
   System.out.println("looping HashMap in Java using EntrySet and java5 for loop");
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

输出结果:

------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop
key: home loan value: citibank
------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop

key: personal loan value: Wells Fargo

使用 EntrySet 和 Java 迭代器

Set<Map.Entry<String, String>> entrySet1 = loans.entrySet();
Iterator<Entry<String, String>> entrySetIterator = entrySet1.iterator();
while (entrySetIterator.hasNext()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating HashMap in Java using EntrySet and Java iterator");
   Entry entry = entrySetIterator.next();
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

输出结果:

Iterating HashMap in Java using EntrySet and Java iterator
key: home loan value: citibank
------------------------------------------------
Iterating HashMap in Java using EntrySet and Java iterator
key: personal loan value: Wells Fargo
加载中
0
MrSharp
MrSharp
前天看到一篇文章,说使用entrySet方式遍历,效率更高,自己测试了一下,确实如此。另外,使用迭代器遍历和不使用有何区别?还请红薯大哥能讲哈
mj4738
mj4738
@红薯 : 同意
红薯
红薯
我不太在意这些方法的性能比较,因为它们不会是我应用中的瓶颈:)
0
嘟嘟的米
嘟嘟的米
确实大部分的应用瓶颈都是在数据库方面 能很好处理这方面性能才是关键
0
BaitaoSi
BaitaoSi

据说这样才是最佳的:

for (Map.Entry<String, JMenu> entry : menuList.entrySet()) {
    mb.add(entry.getValue());
}
出处: http://www.im47.cn/Java/2012/03/25/how-to-write-high-quality-java-source-code/

返回顶部
顶部