Table of Contents
ToggleJava Remove Elements From Map
移除條件相符合的元素,提供幾種 HashMap 刪除元素的方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- Map
| +- RemoveElementsFromMapTest.java
單元測試
Remove an Element from HashMap 移除條件相符合的元素。
removeByKey
建立一個 HashMap ,內有三個元素,移除條件 key 相符合的元素。
@Test
public void removeByKey() {
int expectedSize = 2;
Integer expectedValue = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer removed = map.remove("Grape");
System.out.println(removed);
assertEquals(expectedValue, removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
1
{Kiwifruit=2, Lemon=3}
removeByKeyValue
建立一個 HashMap ,內有三個元素,移除條件 key 、 value 相符合的元素。
@Test
public void removeByKeyValue() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean removed = map.remove("Lemon", 3);
System.out.println(removed);
assertTrue(removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
removed = map.remove("Lemon", 10);
System.out.println(removed);
assertFalse(removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
true
{Grape=1, Kiwifruit=2}
false
{Grape=1, Kiwifruit=2}
keyIteratorRemove
建立一個 HashMap ,內有三個元素,使用 keySet iterator ,在迴圈內移除條件相符合的元素。
@Test
public void keyIteratorRemove() {
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 (Objects.equal("Kiwifruit", e)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, Lemon=3}
valuesIteratorRemove
建立一個 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 (Objects.equal(3, e)) {
it.remove();
}
}
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, 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());
}
{Kiwifruit=2, Lemon=3}
RemoveElementsFromMapTest.java
Java HashMap Remove 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Objects;
public class RemoveElementsFromMapTest {
public RemoveElementsFromMapTest() {
}
@Test
public void removeByKey() {
int expectedSize = 2;
Integer expectedValue = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer removed = map.remove("Grape");
System.out.println(removed);
assertEquals(expectedValue, removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void removeByKeyValue() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean removed = map.remove("Lemon", 3);
System.out.println(removed);
assertTrue(removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
removed = map.remove("Lemon", 10);
System.out.println(removed);
assertFalse(removed);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void keyIteratorRemove() {
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 (Objects.equal("Kiwifruit", 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 (Objects.equal(3, e)) {
it.remove();
}
}
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());
}
}
心得分享
Java HashMap Remove 提供了幾種 HashMap 移除元素方法的操作範例,完成 Remove Elements from an HashMap in Java 刪除元素的需求。