Table of Contents
ToggleHashMap in Java with Examples
只允許 1 個 Null Key 鍵值,不保證依照新增順序保持排序,是一個非同步的操作,HashMap in Java 介紹常見的 containsKey 、 replace 、 entrySet 、 forEach 、 toArray 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- hashmap
| +- HashMapWithExamplesTest.java
單元測試
HashMap Java 提供循環訪問、轉成陣列等操作 Map 中的元素。
entrySet
建立一個 HashMap ,內有三個元素,迴圈取得元素。
@Test
public void entrySet() {
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()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
Grape, 1
Kiwifruit, 2
Lemon, 3
forEach
Java HashMap 建立一個 HashMap ,內有三個元素,迴圈取得元素。
@Test
public void forEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.forEach((k, v) -> System.out.println(k + ", " + v));
}
Grape, 1
Kiwifruit, 2
Lemon, 3
keyForEach
HashMap Java 建立一個 HashMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyForEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().forEach(e -> System.out.println(e));
}
Grape
Kiwifruit
Lemon
keyForEachRemaining
HashMap Java 建立一個 HashMap ,內有三個元素,迴圈取得 key 剩餘元素。
@Test
public void keyForEachRemaining() {
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();
Iterator<String> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
Grape
Kiwifruit
----------
Lemon
keyIterator
Java HashMap 建立一個 HashMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyIterator() {
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()) {
System.out.println(it.next());
}
}
Grape
Kiwifruit
Lemon
valueForEach
Java HashMap 建立一個 HashMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueForEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.values().forEach(System.out::println);
}
1
2
3
valueForEachRemaining
Java HashMap 建立一個 HashMap ,內有三個元素,迴圈取得 value 剩餘元素。
@Test
public void valueForEachRemaining() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Collection<Integer> collection = map.values();
Iterator<Integer> it = collection.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
1
2
----------
3
valueIterator
Java HashMap 建立一個 HashMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueIterator() {
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()) {
System.out.println(it.next());
}
}
1
2
3
keyToArray
建立一個 HashMap ,內有三個元素, key 轉換成 String 陣列。
@Test
public void keyToArray() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
String[] array = new String[map.size()];
map.keySet().toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Grape
Kiwifruit
Lemon
keyStreamToArray
建立一個 HashMap ,內有三個元素,使用 stream , key 轉換成 String 陣列。
@Test
public void keyStreamToArray() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
String[] array = map.keySet().stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Grape
Kiwifruit
Lemon
HashMapWithExamplesTest.java
HashMap in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.hashmap;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class HashMapWithExamplesTest {
@Test
public void entrySet() {
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()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
@Test
public void forEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.forEach((k, v) -> System.out.println(k + ", " + v));
}
@Test
public void keyForEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.keySet().forEach(e -> System.out.println(e));
}
@Test
public void keyForEachRemaining() {
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();
Iterator<String> it = set.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
@Test
public void keyIterator() {
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()) {
System.out.println(it.next());
}
}
@Test
public void valueForEach() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
map.values().forEach(System.out::println);
}
@Test
public void valueForEachRemaining() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
Collection<Integer> collection = map.values();
Iterator<Integer> it = collection.iterator();
int i = 0;
while (it.hasNext()) {
System.out.println(it.next());
if (i == 1) {
break;
}
i++;
}
System.out.println("----------");
it.forEachRemaining(e -> {
System.out.println(e);
});
}
@Test
public void valueIterator() {
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()) {
System.out.println(it.next());
}
}
@Test
public void keyToArray() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
String[] array = new String[map.size()];
map.keySet().toArray(array);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
@Test
public void keyStreamToArray() {
int expectedSize = 3;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Grape", 1);
map.put("Kiwifruit", 2);
map.put("Lemon", 3);
String[] array = map.keySet().stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
}
心得分享
Java HashMap Example 提供了幾種 HashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 Java HashMap 這些方法的操作,可以快速撰寫程式,降低錯誤率,再輔以單元測試驗證,建置高效穩定的服務或系統。