在Java 中操作 Map時(shí)高效遍歷和安全刪除數(shù)據(jù)的方法
在 Java 中操作 Map 時(shí),高效遍歷和安全刪除數(shù)據(jù)可以通過以下方式實(shí)現(xiàn):
一、遍歷 Map 的 4 種高效方式
1. 傳統(tǒng)迭代器(Iterator)
Map<String, Integer> map = new HashMap<>();
map.put("key1", 5);
map.put("key2", 3);
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + ": " + value);
}2. Java 8+ forEach + Lambda
map.forEach((key, value) -> {
System.out.println(key + ": " + value);
});3. 增強(qiáng) for 循環(huán)(遍歷 EntrySet)
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
// ...
}4. Stream API(Java 8+)
map.entrySet().stream()
.filter(entry -> entry.getValue() > 3) // 過濾條件
.forEach(entry -> {
System.out.println(entry.getKey();
});二、安全刪除 Map 中的數(shù)據(jù)
1. 遍歷時(shí)刪除
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
if (entry.getValue() < 3) {
iterator.remove();
}
}
2. Java 8+ removeIf
map.entrySet().removeIf(entry -> entry.getValue() < 3);
3. 直接刪除(已知 Key)
map.remove("key1");三、關(guān)鍵注意事項(xiàng)
避免并發(fā)修改異常遍歷時(shí)直接調(diào)用
map.remove(key)會(huì)導(dǎo)致ConcurrentModificationException,必須使用Iterator.remove()或removeIf。性能優(yōu)化
- 對(duì)
HashMap,優(yōu)先遍歷entrySet()(直接獲取 Key-Value)。 - 對(duì)只讀操作,
forEach和Stream性能接近;需過濾/刪除時(shí)優(yōu)先用removeIf。
- 對(duì)
并發(fā)場景多線程環(huán)境下使用
ConcurrentHashMap并結(jié)合Iterator.remove()或原子操作。
四、完整示例代碼
Map<String, Integer> map = new HashMap<>(Map.of(
"key1", 5,
"key2", 3,
"key3", 2
));
// 遍歷并刪除 value < 3
map.entrySet().removeIf(entry -> entry.getValue() < 3);
// 輸出結(jié)果:{key1=5, key2=3}
System.out.println(map);通過上述方法,可以高效且安全地操作 Java 中的 Map 數(shù)據(jù)結(jié)構(gòu)。
到此這篇關(guān)于在Java 中操作 Map時(shí)高效遍歷和安全刪除數(shù)據(jù)的方法的文章就介紹到這了,更多相關(guān)java map遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java由淺入深細(xì)數(shù)數(shù)組的操作下
數(shù)組對(duì)于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對(duì)數(shù)組的實(shí)現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲(chǔ)固定大小的同類型元素2022-04-04
SpringBoot自動(dòng)配置深入探究實(shí)現(xiàn)原理
在springboot的啟動(dòng)類中可以看到@SpringBootApplication注解,它是SpringBoot的核心注解,也是一個(gè)組合注解。其中@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三個(gè)注解尤為重要。今天我們就來淺析這三個(gè)注解的含義2022-08-08
Java實(shí)現(xiàn)ip地址和int數(shù)字的相互轉(zhuǎn)換
這篇文章主要介紹了Java實(shí)現(xiàn)ip地址和int數(shù)字的相互轉(zhuǎn)換,幫助大家更好的利用Java處理數(shù)據(jù),感興趣的朋友可以了解下2020-09-09
java8 stream多字段排序的實(shí)現(xiàn)
這篇文章主要介紹了java8 stream多字段排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
java使用Jsoup連接網(wǎng)站超時(shí)的解決方法
jsoup是一個(gè)非常好的解析網(wǎng)頁的包,用java開發(fā)的,提供了類似DOM,CSS選擇器的方式來查找和提取文檔中的內(nèi)容,提取文檔內(nèi)容時(shí)會(huì)出現(xiàn)超時(shí)的情況,解決方法可看下文2013-11-11
springboot接收json數(shù)據(jù)時(shí),接收到空值問題
這篇文章主要介紹了springboot接收json數(shù)據(jù)時(shí),接收到空值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
java 中使用maven shade plugin 打可執(zhí)行Jar包
這篇文章主要介紹了java 中使用maven shade plugin 打可執(zhí)行Jar包的相關(guān)資料,需要的朋友可以參考下2017-05-05

