Table of Contents
ToggleLinkedHashMap in Java with Examples
可以有許多 Null Value 值對應到不同的鍵值,但只允許 1 個 Null Key 鍵值,保證依照新增順序保持排序,是一個非同步的操作,LinkedHashMap in Java 介紹常見的 entrySet 、 forEach 、 toArray 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- linkedhashmap
| +- LinkedHashMapWithExamplesTest.java
單元測試
HashMap Java 提供循環訪問、轉成陣列等操作 Map 中的元素。
Fruit
建立 Fruit 物件,覆寫 equals 、 hashCode ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
entrySet
LinkedHashMap Java 建立一個 LinkedHashMap ,內有三個元素,迴圈取得元素。
@Test
public void entrySet() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
for (Map.Entry<String, Fruit> e : map.entrySet()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
Grape, {"name":"Grape","quantity":1.0,"type":1}
Kiwifruit, {"name":"Kiwifruit","quantity":2.0,"type":1}
Lemon, {"name":"Lemon","quantity":3.0,"type":1}
forEach
LinkedHashMap Java 建立一個 LinkedHashMap ,內有三個元素,迴圈取得元素。
@Test
public void forEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.forEach((k, v) -> System.out.println(k + ", " + v));
}
Grape, {"name":"Grape","quantity":1.0,"type":1}
Kiwifruit, {"name":"Kiwifruit","quantity":2.0,"type":1}
Lemon, {"name":"Lemon","quantity":3.0,"type":1}
keyForEach
LinkedHashMap Java 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyForEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.keySet().forEach(e -> System.out.println(e));
}
Grape
Kiwifruit
Lemon
keyForEachRemaining
LinkedHashMap Java 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 key 剩餘元素。
@Test
public void keyForEachRemaining() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
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 LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyIterator() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
Grape
Kiwifruit
Lemon
valueForEach
Java LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueForEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.values().forEach(System.out::println);
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
{"name":"Lemon","quantity":3.0,"type":1}
valueForEachRemaining
Java LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 value 剩餘元素。
@Test
public void valueForEachRemaining() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Collection<Fruit> collection = map.values();
Iterator<Fruit> 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);
});
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
----------
{"name":"Lemon","quantity":3.0,"type":1}
valueIterator
Java LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueIterator() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Iterator<Fruit> it = map.values().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
{"name":"Lemon","quantity":3.0,"type":1}
keyToArray
Java LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素, key 轉換成 String 陣列。
@Test
public void keyToArray() {
int expectedSize = 3;
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
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
Java LinkedHashMap 建立一個 LinkedHashMap ,內有三個元素,使用 stream , key 轉換成 String 陣列。
@Test
public void keyStreamToArray() {
int expectedSize = 3;
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
String[] array = map.keySet().stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
Grape
Kiwifruit
Lemon
LinkedHashMapWithExamplesTest.java
LinkedHashMap in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.linkedhashmap;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class LinkedHashMapWithExamplesTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
public boolean equals(Object object) {
if (!(object instanceof Fruit)) {
return false;
}
if (this == object) {
return true;
}
Fruit other = (Fruit) object;
return new EqualsBuilder().append(getName(), other.getName()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getName()).toHashCode();
}
}
@Test
public void entrySet() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
for (Map.Entry<String, Fruit> e : map.entrySet()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
@Test
public void forEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.forEach((k, v) -> System.out.println(k + ", " + v));
}
@Test
public void keyForEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.keySet().forEach(e -> System.out.println(e));
}
@Test
public void keyForEachRemaining() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
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, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void valueForEach() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
map.values().forEach(System.out::println);
}
@Test
public void valueForEachRemaining() {
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Collection<Fruit> collection = map.values();
Iterator<Fruit> 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, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
Iterator<Fruit> it = map.values().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void keyToArray() {
int expectedSize = 3;
Map<String, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
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, Fruit> map = new LinkedHashMap<>();
map.put("Grape", new Fruit("Grape", 1, 1));
map.put("Kiwifruit", new Fruit("Kiwifruit", 2, 1));
map.put("Lemon", new Fruit("Lemon", 3, 1));
String[] array = map.keySet().stream().toArray(String[]::new);
for (String e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
}
心得分享
Java LinkedHashMap Example 繼承 HashMap ,實作集合框架的 Map 接口,採用雙向鏈結實作功能,保證新增時的順序,提供了幾種 LinkedHashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 Java LinkedHashMap 這些方法的操作,像是: entrySet 、 forEach 、 toArray 等方法。