Table of Contents
ToggleInitialize HashMap in Java
繼承了一個 AbstractMap ,將資料儲存在 ( Key, Value ) 對中,是集合框架的一部分,提供無參數建構子,預設初始容量為 16 ,加載因子為 0.75 , Initialize HashMap Java 初始化集合,會隨著元素增加或移除,大小自動增長或縮小,不能直接用於基本類型,如 int 、 char 等,必須將基本型別其包裝成類別,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- hashmap
| +- InitializeHashMapTest.java
單元測試
Java Initialize HashMap 提供初始化操作集合中的元素。
put
建立一個 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}
doubleBrace
建立一個 HashMap ,初始化三個元素,代表建立並載入一個新的類別,對效能有不良影響。
@Test
public void doubleBrace() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>() {
private static final long serialVersionUID = -5487223135233714632L;
{
put("Grape", 1);
put("Kiwifruit", 2);
put("Lemon", 3);
}
};
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, Kiwifruit=2, Lemon=3}
putAll
Java Initialize HashMap 建立兩個 HashMap ,使用給定的 Map 物件,初始化三個元素。
@Test
public void putAll() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> newMap = new HashMap<String, Integer>();
newMap.putAll(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
{Kiwifruit=2, Lemon=3, Grape=1}
constructor
Java Initialize HashMap 建立一個 HashMap ,初始化三個元素。
@Test
public void constructor() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> newMap = new HashMap<String, Integer>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
{Kiwifruit=2, Lemon=3, Grape=1}
immutableMap
HashMap Initialization Java 建立一個 ImmutableMap 不可變的 Map ,初始化三個元素。
@Test
public void immutableMap() {
int expectedSize = 3;
Map<String, Integer> map = ImmutableMap.of("Grape", 1, "Kiwifruit", 2, "Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{Grape=1, Kiwifruit=2, Lemon=3}
removeThrowException
HashMap Initialization Java 建立一個 ImmutableMap 不可變的 Map ,初始化三個元素, 操作 remove 方法會拋出例外。
@Test(expected = UnsupportedOperationException.class)
public void removeThrowException() {
int expectedSize = 3;
Map<String, Integer> map = ImmutableMap.of("Grape", 1, "Kiwifruit", 2, "Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
map.remove("Grape");
}
java.lang.UnsupportedOperationException
at com.google.common.collect.ImmutableMap.remove(ImmutableMap.java:652)
at org.ruoxue.java_147.map.InitializeHashMapTest.removeThrowException(InitializeHashMapTest.java:78)
InitializeHashMapTest.java
HashMap Initialization Java 新增單元測試,驗證 Java HashMap Initialize 是否符合預期。
package org.ruoxue.java_147.map.hashmap;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
public class InitializeHashMapTest {
@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 doubleBrace() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>() {
private static final long serialVersionUID = -5487223135233714632L;
{
put("Grape", 1);
put("Kiwifruit", 2);
put("Lemon", 3);
}
};
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void putAll() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> newMap = new HashMap<String, Integer>();
newMap.putAll(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
@Test
public void constructor() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Map<String, Integer> newMap = new HashMap<String, Integer>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
@Test
public void immutableMap() {
int expectedSize = 3;
Map<String, Integer> map = ImmutableMap.of("Grape", 1, "Kiwifruit", 2, "Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test(expected = UnsupportedOperationException.class)
public void removeThrowException() {
int expectedSize = 3;
Map<String, Integer> map = ImmutableMap.of("Grape", 1, "Kiwifruit", 2, "Lemon", 3);
System.out.println(map);
assertEquals(expectedSize, map.size());
map.remove("Grape");
}
}
心得分享
Java HashMap Initialize ,將資料存儲在鍵、值對中,不保證新增時的順序,可以通過另一種類型的索引訪問,插入重複鍵,會替換相應鍵的元素,HashMap Initialization Java 提供了幾種 HashMap 初始化的操作範例,使用單元測試驗證產出結果。