Table of Contents
ToggleInitialize TreeMap in Java
初始化 TreeMap 鍵和值不能直接用於基本類型,如 int 、 char 等,必須將基本型別其包裝成類別,才能存入集合,介紹常見的 put 、 putAll 、 doubleBrace 等方法,了解初始化 Map 的不同操作和方法, Initialize TreeMap Java 會隨著元素增加或移除,大小自動增長或縮小,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- treemap
| +- InitializeTreeMapTest.java
單元測試
Java Initialize TreeMap 提供初始化操作 Map 中的元素。
Fruit
Java Initialize TreeMap 建立 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);
}
}
put
Java Initialize TreeMap 建立一個 TreeMap ,初始化三個元素。
@Test
public void put() {
int expectedSize = 3;
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);
assertEquals(expectedSize, map.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
doubleBrace
Java Initialize TreeMap 建立一個 TreeMap ,初始化三個元素,代表建立並載入一個新的類別,對效能有不良影響。
@Test
public void doubleBrace() {
int expectedSize = 3;
Map<Fruit, Integer> map = new TreeMap<Fruit, Integer>() {
private static final long serialVersionUID = -1234223135233714632L;
{
put(new Fruit("Grape", 1, 1), 1);
put(new Fruit("Lemon", 3, 1), 3);
put(new Fruit("Kiwifruit", 2, 1), 2);
}
};
System.out.println(map);
assertEquals(expectedSize, map.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
putAll
Java Initialize TreeMap 建立兩個 TreeMap ,使用給定的 Map 物件,初始化三個元素。
@Test
public void putAll() {
int expectedSize = 3;
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);
Map<Fruit, Integer> newMap = new TreeMap<>();
newMap.putAll(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
constructor
Java Initialize TreeMap 建立一個 TreeMap ,初始化三個元素。
@Test
public void constructor() {
int expectedSize = 3;
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);
Map<Fruit, Integer> newMap = new TreeMap<>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
comparator
TreeMap Initialization Java 建構子傳入 Comparator 參數,建立一個 TreeMap ,初始化三個元素。
@Test
public void comparator() {
int expectedSize = 3;
Comparator<Fruit> quantityComparator = (o, o2) -> Double.compare(o.quantity, o2.quantity);
Map<Fruit, Integer> map = new TreeMap<>(quantityComparator);
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);
assertEquals(expectedSize, map.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
sortedMap
TreeMap Initialization Java 建構子傳入 SortedMap 參數,建立一個 TreeMap ,初始化三個元素。
@Test
public void sortedMap() {
int expectedSize = 3;
SortedMap<Fruit, Integer> map = new ConcurrentSkipListMap<Fruit, Integer>();
map.put(new Fruit("Grape", 1, 1), 1);
map.put(new Fruit("Lemon", 3, 1), 3);
map.put(new Fruit("Kiwifruit", 2, 1), 2);
Map<Fruit, Integer> newMap = new TreeMap<>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}
InitializeTreeMapTest.java
TreeMap Initialization Java 新增單元測試,驗證 Java TreeMap Initialize 是否符合預期。
package org.ruoxue.java_147.map.treemap;
import static org.junit.Assert.*;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
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 InitializeTreeMapTest {
@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 put() {
int expectedSize = 3;
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);
assertEquals(expectedSize, map.size());
}
@Test
public void doubleBrace() {
int expectedSize = 3;
Map<Fruit, Integer> map = new TreeMap<Fruit, Integer>() {
private static final long serialVersionUID = -1234223135233714632L;
{
put(new Fruit("Grape", 1, 1), 1);
put(new Fruit("Lemon", 3, 1), 3);
put(new Fruit("Kiwifruit", 2, 1), 2);
}
};
System.out.println(map);
assertEquals(expectedSize, map.size());
}
@Test
public void putAll() {
int expectedSize = 3;
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);
Map<Fruit, Integer> newMap = new TreeMap<>();
newMap.putAll(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
@Test
public void constructor() {
int expectedSize = 3;
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);
Map<Fruit, Integer> newMap = new TreeMap<>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
@Test
public void comparator() {
int expectedSize = 3;
Comparator<Fruit> quantityComparator = (o, o2) -> Double.compare(o.quantity, o2.quantity);
Map<Fruit, Integer> map = new TreeMap<>(quantityComparator);
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);
assertEquals(expectedSize, map.size());
}
@Test
public void sortedMap() {
int expectedSize = 3;
SortedMap<Fruit, Integer> map = new ConcurrentSkipListMap<Fruit, Integer>();
map.put(new Fruit("Grape", 1, 1), 1);
map.put(new Fruit("Lemon", 3, 1), 3);
map.put(new Fruit("Kiwifruit", 2, 1), 2);
Map<Fruit, Integer> newMap = new TreeMap<>(map);
System.out.println(newMap);
assertEquals(expectedSize, newMap.size());
}
}
心得分享
Java TreeMap Initialize 初始化 Map ,將資料存儲在鍵、值對中,依照鍵排序,是一個非同步的操作,插入重複鍵,會替換相應鍵的元素,TreeMap Initialization Java 提供了幾種 TreeMap 初始化的操作範例,使用單元測試驗證產出結果。