北京网站开发一般多少钱,一个WordPress多个域名,做网站要做相应的app吗,网络建站公司如何做市场Map 集合的遍历与 List 和 Set 集合不同。Map 有两组值#xff0c;因此遍历时可以只遍历值的集合#xff0c;也可以只遍历键的集合#xff0c;也可以同时遍历。
Map 以及实现 Map 的接口类#xff08;如 HashMap、TreeMap、LinkedHashMap、Hashtable 等#xff09;都可以…Map 集合的遍历与 List 和 Set 集合不同。Map 有两组值因此遍历时可以只遍历值的集合也可以只遍历键的集合也可以同时遍历。
Map 以及实现 Map 的接口类如 HashMap、TreeMap、LinkedHashMap、Hashtable 等都可以用以下几种方式遍历。 1、在 for 循环中使用 entries 实现 Map 的遍历最常见和最常用的。
public static void main(String[] args) {MapString, String map new HashMapString, String();map.put(Java入门教程, http://c.biancheng.net/java/);map.put(C语言入门教程, http://c.biancheng.net/c/);for (Map.EntryString, String entry : map.entrySet()) {String mapKey entry.getKey();String mapValue entry.getValue();System.out.println(mapKey mapValue);}
}
2、使用 for-each 循环遍历 key 或者 values一般适用于只需要 Map 中的 key 或者 value 时使用。性能上比 entrySet 较好。
MapString, String map new HashMapString, String();
map.put(Java入门教程, http://c.biancheng.net/java/);
map.put(C语言入门教程, http://c.biancheng.net/c/);
// 打印键集合
for (String key : map.keySet()) {System.out.println(key);
}
// 打印值集合
for (String value : map.values()) {System.out.println(value);
}
3、使用迭代器Iterator遍历 MapString, String map new HashMapString, String();map.put(Java入门教程, http://c.biancheng.net/java/);map.put(C语言入门教程, http://c.biancheng.net/c/);IteratorEntryString, String entries map.entrySet().iterator();while (entries.hasNext()) {EntryString, String entry entries.next();String key entry.getKey();String value entry.getValue();System.out.println(key : value);}
4、通过键找值遍历这种方式的效率比较低因为本身从键取值是耗时的操作。 for(String key : map.keySet()){String value map.get(key);System.out.println(key:value);}