java中HashMap的七種遍歷方式小結(jié)
HashMap遍歷方式分類(lèi)
- HashMap的多種遍歷方式從大體中歸類(lèi) , 可以分為以下4類(lèi) :
- 迭代器(Iterator)
- For Each
- Lambda (JDK 1.8 +)
- Streams API (JDK 1.8 +)
- 但是每種方式又有不同的實(shí)現(xiàn)類(lèi)型 :
- 使用迭代器(Iterator)EntrySet / KeySet 的方式進(jìn)行遍歷;
- 使用 For Each EntrySet / For Each KeySet 的方式進(jìn)行遍歷;
- 使用 Lambda 表達(dá)式的方式進(jìn)行遍歷;
- 使用 Streams API 單線(xiàn)程 / 多線(xiàn)程 的方式進(jìn)行遍歷;
迭代器(Iterator)EntrySet
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
Iterator<Map.Entry<String, String>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
}
迭代器(Iterator)KeySet
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
Iterator<String> iterator = hashMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object val = hashMap.get(key);
System.out.println("key : " + key + "-----" + "val : " + val);
}
For Each EntrySet
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
}
For Each KeySet
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
for (String key : hashMap.keySet()) {
Object val = hashMap.get(key);
System.out.println("key : " + key + "-----" + "val : " + val);
}
Lambda
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.forEach((key , val) -> System.out.println("key : " + key + "-----" + "val : " + val));
Streams API 單線(xiàn)程
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.entrySet().stream().forEach((entry) -> {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
});
Streams API 多線(xiàn)程
HashMap<String , String> hashMap = new HashMap<>();
hashMap.put("1","name");
hashMap.put("2","age");
hashMap.entrySet().stream().parallel().forEach((entry) -> {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key : " + key + "-----" + "val : " + val);
});
我們不能在遍歷Map時(shí)使用map.remove()方法 , 否則就會(huì)拋出異常 :
java.util.ConcurrentModificationException , 這種辦法是非安全的 , 我們可以使用Iterator.remove() ,或者是Lambda 中的 removeIf() , 或者是Stream 中的 filter() 過(guò)濾或者刪除相關(guān)數(shù)據(jù)
到此這篇關(guān)于java中HashMap的七種遍歷方式小結(jié)的文章就介紹到這了,更多相關(guān)java HashMap遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java網(wǎng)絡(luò)編程之TCP程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程之TCP程序設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Java并發(fā)應(yīng)用之任務(wù)執(zhí)行分析
這篇文章主要為大家詳細(xì)介紹了JavaJava并發(fā)應(yīng)用編程中任務(wù)執(zhí)行分析的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫(kù)方式
這篇文章主要介紹了Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫(kù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Dubbo+zookeeper?最簡(jiǎn)單的分布式搭建方案
這篇文章主要介紹了Dubbo+zookeeper?最簡(jiǎn)單的分布式搭建,本例采用?dubbo+zookeeper?搭建分布式系統(tǒng),環(huán)境?jdk1.8,需要的朋友可以參考下2022-04-04
Spring?@Scheduled定時(shí)器注解使用方式
這篇文章主要介紹了Spring?@Scheduled定時(shí)器注解使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

