Table of Contents
ToggleJava HashMap Methods
將資料存儲在鍵、值對中,可以通過另一種類型的索引訪問,插入重複鍵,會替換相應鍵的元素,允許存儲 Null Key,但應該只有一個空鍵物件,不保證新增時的順序,HashMap Java Methods 介紹常見的 put 、 get 、 clear 、 size 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- hashmap
| +- HashMapMethodsTest.java
單元測試
HashMap Java Methods 提供新增、取得、修改、刪除等操作列表中的元素。
put
HashMap Methods Java 建立一個 HashMap ,增加三個元素。
@Test
public void put() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, Kiwifruit=2, Lemon=3}
putIfAbsent
HashMap Methods Java 建立一個 HashMap ,當元素不存在時,新增元素。
@Test
public void putIfAbsent() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.putIfAbsent("Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, Kiwifruit=2, Lemon=3}
get
HashMap Methods Java 建立一個 HashMap ,內有三個元素,取得指定 Key 元素。
@Test
public void get() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer value = map.get("Kiwifruit");
System.out.println(value);
assertEquals(expected, value);
}
2
getOrDefault
HashMap Methods in Java 建立一個 HashMap ,內有三個元素,取得指定 Key 元素,若不存在傳回預設值。
@Test
public void getOrDefault() {
Integer expected = -1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer value = map.getOrDefault("", -1);
System.out.println(value);
assertEquals(expected, value);
}
-1
update
HashMap Functions in Java 建立一個 HashMap ,內有三個元素,更新指定 Key 元素。
@Test
public void update() {
Integer expected = 10;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
Integer put = map.put("Grape", 10);
System.out.println(put);
assertEquals(1, put.intValue());
System.out.println(map);
assertEquals(expected, map.get("Grape"));
}
{Grape=1, Kiwifruit=2, Lemon=3}
1
{Grape=10, Kiwifruit=2, Lemon=3}
remove
HashMap Java Methods 建立一個 HashMap ,內有三個元素,刪除指定 Key 元素。
@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);
map.remove("Grape");
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Kiwifruit=2, Lemon=3}
clear
建立一個 HashMap ,內有三個元素,刪除所有元素。
@Test
public void clear() {
int expectedSize = 0;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.clear();
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{}
size
建立一個 HashMap ,內有三個元素,取得長度。
@Test
public void size() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map.size());
assertEquals(expectedSize, map.size());
}
3
putAll
建立兩個 HashMap ,內各有三個元素,合併成為一個 Map 。
@Test
public void putAll() {
int expectedSize = 6;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
Map<String, Integer> map2 = new HashMap<String, Integer>();
map.put("Grape", 4);
map.put("Kiwifruit", 5);
map.put("Lemon", 6);
map.putAll(map2);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Apple=1, Cherry=3, Grape=4, Kiwifruit=5, Lemon=6, Banana=2}
isEmpty
建立一個 HashMap ,檢查是否為空 Map 。
@Test
public void isEmpty() {
Map<String, Integer> map = new HashMap<String, Integer>();
System.out.println(map.isEmpty());
assertTrue(map.isEmpty());
map.put("Grape", 4);
map.put("Kiwifruit", 5);
map.put("Lemon", 6);
System.out.println(map.isEmpty());
assertFalse(map.isEmpty());
}
true
false
HashMapMethodsTest.java
HashMap Methods Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.hashmap;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class HashMapMethodsTest {
@Test
public void put() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void putIfAbsent() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
Integer put = map.putIfAbsent("Lemon", 3);
System.out.println(put);
assertNull(put);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void get() {
Integer expected = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer value = map.get("Kiwifruit");
System.out.println(value);
assertEquals(expected, value);
}
@Test
public void getOrDefault() {
Integer expected = -1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer value = map.getOrDefault("", -1);
System.out.println(value);
assertEquals(expected, value);
}
@Test
public void update() {
Integer expected = 10;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map);
Integer put = map.put("Grape", 10);
System.out.println(put);
assertEquals(1, put.intValue());
System.out.println(map);
assertEquals(expected, map.get("Grape"));
}
@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);
map.remove("Grape");
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void clear() {
int expectedSize = 0;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.clear();
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void size() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
System.out.println(map.size());
assertEquals(expectedSize, map.size());
}
@Test
public void putAll() {
int expectedSize = 6;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
Map<String, Integer> map2 = new HashMap<String, Integer>();
map.put("Grape", 4);
map.put("Kiwifruit", 5);
map.put("Lemon", 6);
map.putAll(map2);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void isEmpty() {
Map<String, Integer> map = new HashMap<String, Integer>();
System.out.println(map.isEmpty());
assertTrue(map.isEmpty());
map.put("Grape", 4);
map.put("Kiwifruit", 5);
map.put("Lemon", 6);
System.out.println(map.isEmpty());
assertFalse(map.isEmpty());
}
}
心得分享
HashMap Methods in Java 提供了幾種 HashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 HashMap Functions in Java 這些方法的操作,可以快速撰寫程式,降低錯誤率,再輔以單元測試驗證,建置高效穩定的服務或系統。