Table of Contents
ToggleJava Remove Items From Map
迴圈內移除條件相符合的元素,會拋出 ConcurrentModificationException 例外,所以有更多的方法來操作 Map,提供幾種 Remove an Element from Map 刪除元素的方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- RemoveItemsFromMapTest.java
單元測試
Remove an Element from Map 移除條件相符合的元素。
removeThrowException
建立一個 HashMap ,內有三個元素,迴圈內移除條件相符合的元素,會拋出 ConcurrentModificationException 例外。
@Test(expected = ConcurrentModificationException.class)
public void removeThrowException() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if ("Grape".equals(key)) {
map.remove(key);
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1469)
at java.util.HashMap$EntryIterator.next(HashMap.java:1503)
at java.util.HashMap$EntryIterator.next(HashMap.java:1501)
at org.ruoxue.java_147.map.RemoveFromMapTest.removeThrowException(RemoveFromMapTest.java:25)
entrySetRemoveIf
Remove Items from a Map 建立一個 HashMap ,內有三個元素, 移除條件相符合的元素。
@Test
public void entrySetRemoveIf() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.entrySet().removeIf(e -> e.getKey().equals("Grape"));
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Lemon=3, Kiwifruit=2}
keySetRemoveIf
Remove Items from a Map 建立一個 HashMap ,內有三個元素,移除條件相符合的元素。
@Test
public void keySetRemoveIf() {
int expectedSize = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().removeIf(k -> k.equals("Grape") || k.equals("Lemon"));
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Kiwifruit=2}
valuesRemoveIf
Remove Elements from Map 建立一個 HashMap ,內有三個元素,移除條件相符合的元素。
@Test
public void valuesRemoveIf() {
int expectedSize = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.values().removeIf(v -> v == 1 || v == 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Kiwifruit=2}
remove
建立一個 HashMap,內有三個元素,把條件相符合的元素放入新的 Map ,然後新的 Map 迴圈內,移除舊的 Map 條件相符合的元素。
@Test
public void remove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
List<String> willRemove = new ArrayList<>();
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if ("Grape".equals(key)) {
willRemove.add(key);
}
}
for (String e : willRemove) {
map.remove(e);
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Lemon=3, Kiwifruit=2}
entrySetIteratorRemove
Remove Elements from Map 建立一個 HashMap ,內有三個元素,使用 entrySet iterator ,在迴圈內移除條件相符合的元素。
@Test
public void entrySetIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> e = it.next();
String key = e.getKey();
if ("Grape".equals(key)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Lemon=3, Kiwifruit=2}
keySetIteratorRemove
Remove Elements from Map 建立一個 HashMap ,內有三個元素,使用 keySet iterator ,在迴圈內移除條件相符合的元素。
@Test
public void keySetIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String e = it.next();
if ("Grape".equals(e)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Lemon=3, Kiwifruit=2}
valuesIteratorRemove
Remove an Element from Map 建立一個 HashMap ,內有三個元素,使用 values iterator ,在迴圈內移除條件相符合的元素。
@Test
public void valuesIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<Integer> it = map.values().iterator();
while (it.hasNext()) {
Integer e = it.next();
if (1 == e.intValue()) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Lemon=3, Kiwifruit=2}
filterCollect
建立一個 HashMap,內有三個元素,把條件相符合的元素,建立一個新的 Map 。
@Test
public void filterCollect() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> result = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if (!"Grape".equals(key)) {
result.put(e.getKey(), e.getValue());
}
}
System.out.println(result);
assertEquals(expectedSize, result.size());
}
{Lemon=3, Kiwifruit=2}
streamCollect
建立一個 HashMap,內有三個元素,使用 stream ,把條件相符合的元素,建立一個新的 Map 。
@Test
public void streamCollect() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> result = map.entrySet().stream().filter(e -> !"Grape".equals(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
{Lemon=3, Kiwifruit=2}
RemoveItemsFromMapTest.java
Remove Elements from Map 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Test;
public class RemoveItemsFromMapTest {
public RemoveItemsFromMapTest() {
}
@Test(expected = ConcurrentModificationException.class)
public void removeThrowException() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if ("Grape".equals(key)) {
map.remove(key);
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void entrySetRemoveIf() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.entrySet().removeIf(e -> e.getKey().equals("Grape"));
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void keySetRemoveIf() {
int expectedSize = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().removeIf(k -> k.equals("Grape") || k.equals("Lemon"));
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void valuesRemoveIf() {
int expectedSize = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.values().removeIf(v -> v == 1 || v == 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void remove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
List<String> willRemove = new ArrayList<>();
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if ("Grape".equals(key)) {
willRemove.add(key);
}
}
for (String e : willRemove) {
map.remove(e);
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void entrySetIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> e = it.next();
String key = e.getKey();
if ("Grape".equals(key)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void keySetIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String e = it.next();
if ("Grape".equals(e)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void valuesIteratorRemove() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Iterator<Integer> it = map.values().iterator();
while (it.hasNext()) {
Integer e = it.next();
if (1 == e.intValue()) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void filterCollect() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> result = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
if (!"Grape".equals(key)) {
result.put(e.getKey(), e.getValue());
}
}
System.out.println(result);
assertEquals(expectedSize, result.size());
}
@Test
public void streamCollect() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> result = map.entrySet().stream().filter(e -> !"Grape".equals(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result);
assertEquals(expectedSize, result.size());
}
}
心得分享
Remove Elements from Map 提供了幾種 HashMap 移除元素方法的操作範例,可以刪除元素或是重新產生一個新的 Map ,完成 Remove Items from a Map 刪除元素的需求。