您的位置:首页 > 其它

hashMap的三种遍历方式

2017-11-27 16:17 204 查看
public void testClass(){
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("1", "1");
hashMap.put("2", "2");
Set<Entry<String, String>> entrySet = hashMap.entrySet();
Iterator<Entry<String, String>> iterator = entrySet.iterator();
//1.迭代器实现 效率高
while(iterator.hasNext()){
Entry<String, String> entry = iterator.next();
String key = entry.getKey();
System.out.println(key);
String value = entry.getValue();
System.out.println(value);
}
//2.entrySet
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
//3.keySet
Set<String> keySet = hashMap.keySet();
for (String entry : keySet) {
System.out.println(hashMap.get(entry));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hashmap 遍历