Table of Contents
ToggleJava LinkedHashMap Class
根據 Key 的 HashCode 值儲存資料,具有很快的訪問速度,最多允許一條記錄的 Key 為 Null,保證依照新增順序保持排序, LinkedHashMap Class 介紹常見的 containsKey 、 stream 、 replaceAll 、 merge 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- linkedhashmap
| +- LinkedHashMapClassTest.java
單元測試
LinkedHashMap Class 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();
}
}
containsKey
LinkedHashMap Class Java 建立一個 LinkedHashMap ,增加三個元素,檢查是否包含鍵值。
@Test
public void containsKey() {
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));
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
true
containsValue
LinkedHashMap Class Java 建立一個 LinkedHashMap ,增加三個元素,檢查是否包含值。
@Test
public void containsValue() {
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));
boolean containsValue = map.containsValue(new Fruit("Grape", 1, 1));
System.out.println(containsValue);
assertTrue(containsValue);
}
true
stream
LinkedHashMap Class Java 建立一個 LinkedHashMap ,內有三個元素,使用串流,取得長度小於 6 的元素。
@Test
public void stream() {
int expectedSize = 2;
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().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
[Grape, Lemon]
parallelStream
LinkedHashMap Class Java 建立一個 LinkedHashMap ,內有三個元素,使用並行串流。
@Test
public void parallelStream() {
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().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
Grape
Lemon
Kiwifruit
----------
Grape
Kiwifruit
Lemon
replace
Linked Hash Map Class in Java 建立一個 LinkedHashMap ,內有三個元素,取代指定 Key 元素的值。
@Test
public void replace() {
double expected = 10d;
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.replace("Grape", new Fruit("Grape", 10, 1));
System.out.println(map);
assertEquals(expected, map.get("Grape").getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
replaceAll
Linked Hash Map Class in Java 建立一個 LinkedHashMap ,內有三個元素,取代所有元素的值。
@Test
public void replaceAll() {
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.replaceAll((k, v) -> {
v.setQuantity(v.getQuantity() * 10);
return v;
});
System.out.println(map);
}
{Grape={"name":"Grape","quantity":10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":20.0,"type":1}, Lemon={"name":"Lemon","quantity":30.0,"type":1}}
merge
Linked Hash Map Class in Java 建立一個 LinkedHashMap ,內有三個元素,指定 key 值,合併 value 舊值與新值。
@Test
public void merge() {
double expected = 11d;
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));
Fruit replaced = map.merge("Grape", new Fruit("Grape", 10, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(expected, replaced.getQuantity(), 0);
replaced = map.merge("Papaya", new Fruit("Papaya", 4, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(4d, replaced.getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":11.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
{Grape={"name":"Grape","quantity":11.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}, Papaya={"name":"Papaya","quantity":4.0,"type":1}}
LinkedHashMapClassTest.java
Linked Hash Map Class in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.linkedhashmap;
import static org.junit.Assert.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
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 LinkedHashMapClassTest {
@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 containsKey() {
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));
boolean containsKey = map.containsKey("Lemon");
System.out.println(containsKey);
assertTrue(containsKey);
}
@Test
public void containsValue() {
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));
boolean containsValue = map.containsValue(new Fruit("Grape", 1, 1));
System.out.println(containsValue);
assertTrue(containsValue);
}
@Test
public void stream() {
int expectedSize = 2;
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().stream().filter(e -> e.length() < 6).collect(Collectors.toSet());
System.out.println(set);
assertEquals(expectedSize, set.size());
}
@Test
public void parallelStream() {
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().parallelStream().forEach(System.out::println);
System.out.println("----------");
map.keySet().parallelStream().forEachOrdered(System.out::println);
}
@Test
public void replace() {
double expected = 10d;
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.replace("Grape", new Fruit("Grape", 10, 1));
System.out.println(map);
assertEquals(expected, map.get("Grape").getQuantity(), 0);
}
@Test
public void replaceAll() {
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.replaceAll((k, v) -> {
v.setQuantity(v.getQuantity() * 10);
return v;
});
System.out.println(map);
}
@Test
public void merge() {
double expected = 11d;
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));
Fruit replaced = map.merge("Grape", new Fruit("Grape", 10, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(expected, replaced.getQuantity(), 0);
replaced = map.merge("Papaya", new Fruit("Papaya", 4, 1), (oldValue, newValue) -> {
newValue.setQuantity(oldValue.getQuantity() + newValue.getQuantity());
return newValue;
});
System.out.println(map);
assertEquals(4d, replaced.getQuantity(), 0);
}
}
心得分享
Java LinkedHashMap Class Example 實現 Map 接口,繼承 HashMap ,資料儲存以 Key 、 Value 的方式對應,透過 Key 取得 Value ,如果插入重複 Key ,將會替換相對應 Key 的 Value ,可以有任意數量 Value 為 Null 值, Linked Hash Map Class in Java 提供 containsKey 、 stream 、 replaceAll 、 merge 等操作範例。