forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5162 : HashMap Entry Removal (eugenp#11276)
- Loading branch information
1 parent
92ad02a
commit 5ec97aa
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...ns-maps-3/src/main/java/com/baeldung/map/hashmap/entryremoval/RemoveEntryApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.baeldung.map.hashmap.entryremoval; | ||
|
||
import java.util.ConcurrentModificationException; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class RemoveEntryApplication { | ||
|
||
public static void main(String[] args) { | ||
|
||
HashMap<String, String> foodItemTypeMap = new HashMap<>(); | ||
foodItemTypeMap.put("Apple", "Fruit"); | ||
foodItemTypeMap.put("Grape", "Fruit"); | ||
foodItemTypeMap.put("Mango", "Fruit"); | ||
foodItemTypeMap.put("Carrot", "Vegetable"); | ||
foodItemTypeMap.put("Potato", "Vegetable"); | ||
foodItemTypeMap.put("Spinach", "Vegetable"); | ||
// Current Map Status: {Potato=Vegetable, Apple=Fruit, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable} | ||
|
||
foodItemTypeMap.remove("Apple"); | ||
// Current Map Status: {Potato=Vegetable, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable} | ||
|
||
foodItemTypeMap.remove("Grape", "Vegetable"); | ||
// Current Map Status: {Potato=Vegetable, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable} | ||
|
||
try { | ||
for (Entry<String, String> item : foodItemTypeMap.entrySet()) { | ||
if (item.getKey() | ||
.equals("Potato")) { | ||
foodItemTypeMap.remove(item.getKey()); | ||
} | ||
} | ||
} catch (ConcurrentModificationException e) { | ||
System.out.println("Exception occured while updating map: " + e.toString()); | ||
} | ||
|
||
foodItemTypeMap.entrySet() | ||
.removeIf(entry -> entry.getKey() | ||
.equals("Grape")); | ||
// Current Map Status: {Carrot=Vegetable, Mango=Fruit, Spinach=Vegetable} | ||
|
||
Iterator<Entry<String, String>> iterator = foodItemTypeMap.entrySet() | ||
.iterator(); | ||
while (iterator.hasNext()) { | ||
if (iterator.next() | ||
.getKey() | ||
.equals("Carrot")) | ||
iterator.remove(); | ||
} | ||
// Current Map Status: {Mango=Fruit, Spinach=Vegetable} | ||
|
||
// Use ConcurrentHashMap | ||
ConcurrentHashMap<String, String> foodItemTypeConcMap = new ConcurrentHashMap<>(); | ||
foodItemTypeConcMap.put("Apple", "Fruit"); | ||
foodItemTypeConcMap.put("Grape", "Fruit"); | ||
foodItemTypeConcMap.put("Mango", "Fruit"); | ||
foodItemTypeConcMap.put("Carrot", "Vegetable"); | ||
foodItemTypeConcMap.put("Potato", "Vegetable"); | ||
foodItemTypeConcMap.put("Spinach", "Vegetable"); | ||
|
||
for (Entry<String, String> item : foodItemTypeConcMap.entrySet()) { | ||
if (item.getKey() != null && item.getKey() | ||
.equals("Potato")) { | ||
foodItemTypeConcMap.remove(item.getKey()); | ||
} | ||
} | ||
|
||
// foodItemTypeConcMap : {Apple=Fruit, Carrot=Vegetable, Grape=Fruit, Mango=Fruit, Spinach=Vegetable} | ||
|
||
} | ||
|
||
} |