Table of Contents
ToggleJava LinkedHashMap compute Method
使用所提供的映射方法 BiFunction 為指定鍵計算一個新值,該鍵可能存在或不存在於 Map 中, Compute Java LinkedHashMap 介紹常見的 compute 、 computeIfAbsent 、 computeIfPresent 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- linkedhashmap
| +- LinkedHashMapComputeTest.java
單元測試
LinkedHashMap Compute 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();
}
}
@Test
public void compute() {
double expected = 2d;
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));
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key).getQuantity(), 0);
}
compute
LinkedHashMap Compute Java 建立一個 LinkedHashMap ,增加三個元素,計算符合條件的新值。
@Test
public void compute() {
double expected = 2d;
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));
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key).getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
{Grape={"name":"Grape","quantity":2.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
computeCount
LinkedHashMap Compute Java 建立一個 LinkedHashMap ,計算 String 個數。
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (int i = 0; i < value.length(); i++) {
String key = String.valueOf(value.charAt(i));
map.compute(key, (k, v) -> {
v = (v == null ? 1 : v + 1);
return v;
});
}
System.out.println(map);
}
{H=1, e=2, l=3, o=2, =3, W=1, r=2, d=1, ,=1, J=1, a=3, v=1, L=1, n=1}
computeIfAbsent
LinkedHashMap Compute Java 建立一個 LinkedHashMap ,內有三個元素,計算不存在的 Key 新值。
@Test
public void computeIfAbsent() {
double expected = 4d;
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));
System.out.println(map);
String key = "Mango";
Fruit result = map.computeIfAbsent(key, k -> new Fruit("Mango", 4, 1));
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Grape";
result = map.computeIfAbsent(key, k -> new Fruit("Grape", 2, 1));
assertEquals(1d, result.getQuantity(), 0);
}
{Grape={"name":"Grape","quantity":1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
{Grape={"name":"Grape","quantity":1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}, Mango={"name":"Mango","quantity":4.0,"type":1}}
computeIfPresent
LinkedHashMap Compute Java 建立一個 LinkedHashMap ,內有三個元素,計算已存在的 Key 新值。
@Test
public void computeIfPresent() {
double expected = 2d;
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));
System.out.println(map);
String key = "Grape";
Fruit result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(4);
return v;
});
assertNull(result);
}
{Grape={"name":"Grape","quantity":1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
{Grape={"name":"Grape","quantity":2.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}
LinkedHashMapComputeTest.java
LinkedHashMap Compute Example 新增單元測試,驗證 Java LinkedHashMap Compute 是否符合預期。
package org.ruoxue.java_147.map.linkedhashmap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.LinkedHashMap;
import java.util.Map;
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 LinkedHashMapComputeTest {
@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 compute() {
double expected = 2d;
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));
System.out.println(map);
String key = "Grape";
map.compute(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, map.get(key).getQuantity(), 0);
}
@Test
public void computeCount() {
String value = "Hello World, Java Learn";
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (int i = 0; i < value.length(); i++) {
String key = String.valueOf(value.charAt(i));
map.compute(key, (k, v) -> {
v = (v == null ? 1 : v + 1);
return v;
});
}
System.out.println(map);
}
@Test
public void computeIfAbsent() {
double expected = 4d;
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));
System.out.println(map);
String key = "Mango";
Fruit result = map.computeIfAbsent(key, k -> new Fruit("Mango", 4, 1));
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Grape";
result = map.computeIfAbsent(key, k -> new Fruit("Grape", 2, 1));
assertEquals(1d, result.getQuantity(), 0);
}
@Test
public void computeIfPresent() {
double expected = 2d;
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));
System.out.println(map);
String key = "Grape";
Fruit result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(v.getQuantity() + 1);
return v;
});
System.out.println(map);
assertEquals(expected, result.getQuantity(), 0);
key = "Mango";
result = map.computeIfPresent(key, (k, v) -> {
v.setQuantity(4);
return v;
});
assertNull(result);
}
}
心得分享
Java LinkedHashMap Compute 提供了幾種 LinkedHashMap 常見方法的操作範例,在應用上相當廣泛,熟悉 LinkedHashMap Compute Example 這些方法的操作,例如: compute 、 computeIfAbsent 、 computeIfPresent 等方法,可以讓程式碼更加地簡潔及容易維護。