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