Table of Contents
ToggleJava HashMap Class
儲存的內容是 Key-Value 映射,實現 Map 接口,根據 Key 的 HashCode 值儲存資料,具有很快的訪問速度,最多允許一條記錄的 Key 為 Null,不保證依照新增順序保持排序,是一個非同步的操作,HashMap Class 介紹常見的 containsKey 、 stream 、 replaceAll 、 merge 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- hashmap
| +- HashMapClassTest.java
單元測試
HashMap Class Java 提供檢查是否包含鍵值、取代、轉成陣列、合併等操作 Map 中的元素。
containsKey
建立一個 HashMap ,增加三個元素,檢查是否包含鍵值。
@Test
public void containsKey() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
true
containsValue
HashMap Class Java 建立一個 HashMap ,增加三個元素,檢查是否包含值。
@Test
public void containsValue() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean containsValue = map.containsValue(3);
System.out.println(containsValue);
assertTrue(containsValue);
}
true
stream
HashMap Class Java 建立一個 HashMap ,內有三個元素,執行串流,取得長度小於 6 的元素。
@Test
public void stream() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Set<String> set = map.keySet().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Grape, Lemon]
parallelStream
HashMap Class Java 建立一個 HashMap ,內有三個元素,並行執行串流。
@Test
public void parallelStream() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
Kiwifruit
Lemon
Grape
----------
Grape
Kiwifruit
Lemon
replace
Hash Map Class in Java 建立一個 HashMap ,內有三個元素,取代指定 Key 元素的值。
@Test
public void replace() {
Integer expected = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer replaced = map.replace("Grape", 10);
System.out.println(map);
assertEquals(expected, replaced);
boolean repl = map.replace("Grape", 10, 1);
System.out.println(map);
assertEquals(true, repl);
}
{Grape=10, Kiwifruit=2, Lemon=3}
{Grape=1, Kiwifruit=2, Lemon=3}
replaceAll
Hash Map Class in Java 建立一個 HashMap ,內有三個元素,取代所有元素的值。
@Test
public void replaceAll() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.replaceAll((k, v) -> {
v = v * 10;
return v;
});
System.out.println(map);
}
{Grape=10, Kiwifruit=20, Lemon=30}
merge
Hash Map Class in Java 建立一個 HashMap ,內有三個元素,指定 key 值,合併 value 舊值與新值。
@Test
public void merge() {
Integer expected = 11;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer replaced = map.merge("Grape", 10, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map);
assertEquals(expected, replaced);
replaced = map.merge("Papaya", 4, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map);
assertEquals(4, replaced.intValue());
}
{Grape=11, Kiwifruit=2, Lemon=3}
{Papaya=4, Grape=11, Kiwifruit=2, Lemon=3}
HashMapClassTest.java
Hash Map Class in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.hashmap;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Test;
public class HashMapClassTest {
@Test
public void containsKey() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
@Test
public void containsValue() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
boolean containsValue = map.containsValue(3);
System.out.println(containsValue);
assertTrue(containsValue);
}
@Test
public void stream() {
int expectedSize = 2;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Set<String> set = map.keySet().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void parallelStream() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
@Test
public void replace() {
Integer expected = 1;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer replaced = map.replace("Grape", 10);
System.out.println(map);
assertEquals(expected, replaced);
boolean repl = map.replace("Grape", 10, 1);
System.out.println(map);
assertEquals(true, repl);
}
@Test
public void replaceAll() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.replaceAll((k, v) -> {
v = v * 10;
return v;
});
System.out.println(map);
}
@Test
public void merge() {
Integer expected = 11;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Integer replaced = map.merge("Grape", 10, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map);
assertEquals(expected, replaced);
replaced = map.merge("Papaya", 4, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map);
assertEquals(4, replaced.intValue());
}
}
心得分享
Java HashMap Class Example 資料儲存以 Key 、 Value 的方式對應,透過 Key 取得 Value ,如果插入重複 Key ,將會替換相對應 Key 的 Value ,可以有任意數量 Value 為 Null 值,提供 Hash Map Class in Java 新增、修改、刪除及循環訪問等操作範例。