Table of Contents
ToggleTreeMap in Java with Examples
可以有許多 Null Value 值對應到不同的鍵值,不允許儲存 Null Key 鍵值,依照鍵排序,是一個非同步的操作,TreeMap in Java 介紹常見的 entrySet 、 forEach 、 toArray 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- map
| +- treemap
| +- TreeMapWithExamplesTest.java
單元測試
TreeMap Java 提供循環訪問、轉成陣列等操作 Map 中的元素。
Fruit
TreeMap Java 建立 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);
}
}
entrySet
TreeMap Java 建立一個 TreeMap ,內有三個元素,迴圈取得元素。
@Test
public void entrySet() {
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);
for (Map.Entry<Fruit, Integer> e : map.entrySet()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
{"name":"Grape","quantity":1.0,"type":1}, 1
{"name":"Kiwifruit","quantity":2.0,"type":1}, 2
{"name":"Lemon","quantity":3.0,"type":1}, 3
forEach
TreeMap Java 建立一個 TreeMap ,內有三個元素,迴圈取得元素。
@Test
public void forEach() {
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.forEach((k, v) -> System.out.println(k + ", " + v));
}
{"name":"Grape","quantity":1.0,"type":1}, 1
{"name":"Kiwifruit","quantity":2.0,"type":1}, 2
{"name":"Lemon","quantity":3.0,"type":1}, 3
keyForEach
TreeMap Java 建立一個 TreeMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyForEach() {
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.keySet().forEach(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}
keyForEachRemaining
TreeMap Java 建立一個 TreeMap ,內有三個元素,迴圈取得 key 剩餘元素。
@Test
public void keyForEachRemaining() {
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);
Set<Fruit> set = map.keySet();
Iterator<Fruit> 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);
});
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
----------
{"name":"Lemon","quantity":3.0,"type":1}
keyIterator
Java TreeMap 建立一個 TreeMap ,內有三個元素,迴圈取得 key 元素。
@Test
public void keyIterator() {
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);
Iterator<Fruit> it = map.keySet().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}
valueForEach
Java TreeMap 建立一個 TreeMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueForEach() {
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.values().forEach(System.out::println);
}
1
2
3
valueForEachRemaining
Java TreeMap 建立一個 TreeMap ,內有三個元素,迴圈取得 value 剩餘元素。
@Test
public void valueForEachRemaining() {
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);
Collection<Integer> collection = map.values();
Iterator<Integer> 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);
});
}
1
2
----------
3
valueIterator
Java TreeMap 建立一個 TreeMap ,內有三個元素,迴圈取得 value 元素。
@Test
public void valueIterator() {
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);
Iterator<Integer> it = map.values().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
1
2
3
keyToArray
Java TreeMap 建立一個 TreeMap ,內有三個元素, key 轉換成 String 陣列。
@Test
public void keyToArray() {
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);
Fruit[] array = new Fruit[map.size()];
map.keySet().toArray(array);
for (Fruit e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
{"name":"Lemon","quantity":3.0,"type":1}
keyStreamToArray
Java TreeMap 建立一個 TreeMap ,內有三個元素,使用 stream , key 轉換成 String 陣列。
@Test
public void keyStreamToArray() {
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);
Fruit[] array = map.keySet().stream().toArray(Fruit[]::new);
for (Fruit e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
{"name":"Grape","quantity":1.0,"type":1}
{"name":"Kiwifruit","quantity":2.0,"type":1}
{"name":"Lemon","quantity":3.0,"type":1}
TreeMapWithExamplesTest.java
TreeMap in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.map.treemap;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.TreeMap;
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 TreeMapWithExamplesTest {
@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 entrySet() {
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);
for (Map.Entry<Fruit, Integer> e : map.entrySet()) {
System.out.println(e.getKey() + ", " + e.getValue());
}
}
@Test
public void forEach() {
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.forEach((k, v) -> System.out.println(k + ", " + v));
}
@Test
public void keyForEach() {
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.keySet().forEach(e -> System.out.println(e));
}
@Test
public void keyForEachRemaining() {
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);
Set<Fruit> set = map.keySet();
Iterator<Fruit> 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<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);
Iterator<Fruit> it = map.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void valueForEach() {
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.values().forEach(System.out::println);
}
@Test
public void valueForEachRemaining() {
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);
Collection<Integer> collection = map.values();
Iterator<Integer> 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<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);
Iterator<Integer> it = map.values().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
@Test
public void keyToArray() {
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);
Fruit[] array = new Fruit[map.size()];
map.keySet().toArray(array);
for (Fruit e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
@Test
public void keyStreamToArray() {
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);
Fruit[] array = map.keySet().stream().toArray(Fruit[]::new);
for (Fruit e : array) {
System.out.println(e);
}
assertEquals(expectedSize, array.length);
}
}
心得分享
Java TreeMap Example 實作集合框架的 SortedMap 接口,提供依照鍵排序,提供了幾種 TreeMap 常見方法的操作範例,在應用上相當廣泛,熟悉 Java TreeMap 這些方法的操作,像是: entrySet 、 forEach 、 toArray 等方法。