Java TreeMap Methods - Java 147

Java TreeMap Methods – Java 147

Java TreeMap Methods

不允許儲存空鍵,但可以儲存空值,提供自訂 Comparator 依照鍵排序,介紹常見的 put 、 get 、 clear 、 size 等方法,將資料存儲在鍵、值對中,可以通過另一種類型的索引訪問,當插入重複鍵時,會替換相應鍵的元素, TreeMap Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- map
       |                   +- treemap
       |                       +- TreeMapMethodsTest.java   

單元測試

TreeMap Java Methods 提供新增、取得、修改、刪除等操作 Map 中的元素。

Fruit

TreeMap Methods 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);
		}
	}

put

TreeMap Methods Java 建立一個 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}

putIfAbsent

TreeMap Methods Java 建立一個 TreeMap ,當元素不存在時,新增元素。

	@Test
	public void putIfAbsent() {
		int expectedSize = 3;
		Map<Fruit, Integer> map = new TreeMap<>();
		map.put(new Fruit("Grape", 1, 1), 1);
		Integer put = map.putIfAbsent(new Fruit("Lemon", 3, 1), 3);
		System.out.println(put);
		assertNull(put);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
null
{{"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}

get

TreeMap Methods Java 建立一個 TreeMap ,內有三個元素,取得指定 Key 元素。

	@Test
	public void get() {
		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);
		Fruit key = new Fruit("Kiwifruit", 2, 1);
		map.put(key, 2);
		Integer value = map.get(key);
		System.out.println(value);
		assertEquals(expected, value);
	}
2

getOrDefault

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,取得指定 Key 元素,若不存在傳回預設值。

	@Test
	public void getOrDefault() {
		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);
		Integer result = map.getOrDefault(new Fruit("Empty", 0, 0), new Integer(10));
		System.out.println(result);
		assertNotNull(result);
	}
10

update

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,更新指定 Key 元素。

	@Test
	public void update() {
		Integer expected = 10;
		Map<Fruit, Integer> map = new TreeMap<>();
		Fruit key = new Fruit("Grape", 1, 1);
		map.put(key, 1);
		map.put(new Fruit("Lemon", 3, 1), 3);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		System.out.println(map);

		Integer put = map.put(key, new Integer(10));
		assertEquals(1, put.intValue());
		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}=10, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}

remove

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,刪除指定 Key 元素。

	@Test
	public void remove() {
		int expectedSize = 2;
		Map<Fruit, Integer> map = new TreeMap<>();
		Fruit key = new Fruit("Grape", 1, 1);
		map.put(key, 1);
		map.put(new Fruit("Lemon", 3, 1), 3);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		map.remove(key);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{{"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}

clear

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,刪除所有元素。

	@Test
	public void clear() {
		int expectedSize = 0;
		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.clear();
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{}

size

TreeMap Methods in Java 建立一個 TreeMap ,內有三個元素,取得長度。

	@Test
	public void size() {
		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.size());
		assertEquals(expectedSize, map.size());
	}
3

putAll

建立兩個 TreeMap ,內各有三個元素,合併成為一個 Map 。

	@Test
	public void putAll() {
		int expectedSize = 6;
		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.put(new Fruit("Apple", 4, 1), 4);
		newMap.put(new Fruit("Cherry", 6, 1), 6);
		newMap.put(new Fruit("Banana", 5, 1), 5);

		map.putAll(newMap);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{{"name":"Apple","quantity":4.0,"type":1}=4, {"name":"Banana","quantity":5.0,"type":1}=5, {"name":"Cherry","quantity":6.0,"type":1}=6, {"name":"Grape","quantity":1.0,"type":1}=1, {"name":"Kiwifruit","quantity":2.0,"type":1}=2, {"name":"Lemon","quantity":3.0,"type":1}=3}

isEmpty

建立一個 TreeMap ,檢查是否為空 Map 。

	@Test
	public void isEmpty() {
		Map<Fruit, Integer> map = new TreeMap<>();
		System.out.println(map.isEmpty());
		assertTrue(map.isEmpty());
		map.put(new Fruit("Grape", 1, 1), 1);
		System.out.println(map.isEmpty());
		assertFalse(map.isEmpty());
	}
true
false

TreeMapMethodsTest.java

TreeMap Methods in Java 新增單元測試,驗證 TreeMap Functions in Java 是否符合預期。

package org.ruoxue.java_147.map.treemap;

import static org.junit.Assert.*;

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 TreeMapMethodsTest {

	@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 putIfAbsent() {
		int expectedSize = 3;
		Map<Fruit, Integer> map = new TreeMap<>();
		map.put(new Fruit("Grape", 1, 1), 1);
		Integer put = map.putIfAbsent(new Fruit("Lemon", 3, 1), 3);
		System.out.println(put);
		assertNull(put);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void get() {
		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);
		Fruit key = new Fruit("Kiwifruit", 2, 1);
		map.put(key, 2);
		Integer value = map.get(key);
		System.out.println(value);
		assertEquals(expected, value);
	}

	@Test
	public void getOrDefault() {
		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);
		Integer result = map.getOrDefault(new Fruit("Empty", 0, 0), new Integer(10));
		System.out.println(result);
		assertNotNull(result);
	}

	@Test
	public void update() {
		Integer expected = 10;
		Map<Fruit, Integer> map = new TreeMap<>();
		Fruit key = new Fruit("Grape", 1, 1);
		map.put(key, 1);
		map.put(new Fruit("Lemon", 3, 1), 3);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		System.out.println(map);

		Integer put = map.put(key, new Integer(10));
		assertEquals(1, put.intValue());
		System.out.println(map);
		assertEquals(expected, map.get(key));
	}

	@Test
	public void remove() {
		int expectedSize = 2;
		Map<Fruit, Integer> map = new TreeMap<>();
		Fruit key = new Fruit("Grape", 1, 1);
		map.put(key, 1);
		map.put(new Fruit("Lemon", 3, 1), 3);
		map.put(new Fruit("Kiwifruit", 2, 1), 2);
		map.remove(key);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void clear() {
		int expectedSize = 0;
		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.clear();
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void size() {
		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.size());
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void putAll() {
		int expectedSize = 6;
		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.put(new Fruit("Apple", 4, 1), 4);
		newMap.put(new Fruit("Cherry", 6, 1), 6);
		newMap.put(new Fruit("Banana", 5, 1), 5);

		map.putAll(newMap);
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}

	@Test
	public void isEmpty() {
		Map<Fruit, Integer> map = new TreeMap<>();
		System.out.println(map.isEmpty());
		assertTrue(map.isEmpty());
		map.put(new Fruit("Grape", 1, 1), 1);
		System.out.println(map.isEmpty());
		assertFalse(map.isEmpty());
	}
}

心得分享

TreeMap Functions in Java 實作集合框架的 SortedMap 接口,提供依照鍵排序,熟悉 TreeMap Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言