Java LinkedHashMap Methods - Java 147

Java LinkedHashMap Methods – Java 147

Java LinkedHashMap Methods

允許儲存空鍵、空值,但只能有一個 Null Key ,保證新增時的順序,介紹常見的 put 、 get 、 clear 、 size 等方法,將資料存儲在鍵、值對中,可以通過另一種類型的索引訪問,當插入重複鍵時,會替換相應鍵的元素, LinkedHashMap Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- map
       |                   +- linkedhashmap
       |                       +- LinkedHashMapMethodsTest.java   

單元測試

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

Fruit

LinkedHashMap Methods Java 建立 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();
		}
	}

put

LinkedHashMap Methods Java 建立一個 LinkedHashMap ,增加三個元素。

	@Test
	public void put() {
		int expectedSize = 3;
		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);
		assertEquals(expectedSize, map.size());
	}
{Grape={"name":"Grape","quantity":1.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}

putIfAbsent

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

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

get

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

	@Test
	public void get() {
		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));
		Fruit value = map.get("Kiwifruit");
		System.out.println(value);
		assertEquals(expected, value.getQuantity(), 0);
	}
{"name":"Kiwifruit","quantity":2.0,"type":1}

getOrDefault

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

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

update

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

	@Test
	public void update() {
		double expected = 10d;
		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);

		Fruit put = map.put("Grape", new Fruit("Grape", 10, 1));
		assertEquals(1d, put.getQuantity(), 0);
		System.out.println(map);
		assertEquals(expected, map.get("Grape").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":10.0,"type":1}, Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}

remove

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

	@Test
	public void remove() {
		int expectedSize = 2;
		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));
		map.remove("Grape");
		System.out.println(map);
		assertEquals(expectedSize, map.size());
	}
{Kiwifruit={"name":"Kiwifruit","quantity":2.0,"type":1}, Lemon={"name":"Lemon","quantity":3.0,"type":1}}

clear

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

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

size

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

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

putAll

LinkedHashMap Functions in Java 建立兩個 LinkedHashMap ,內各有三個元素,合併成為一個 Map 。

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

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

isEmpty

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

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

LinkedHashMapMethodsTest.java

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

package org.ruoxue.java_147.map.linkedhashmap;

import static org.junit.Assert.*;

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

	@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 put() {
		int expectedSize = 3;
		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);
		assertEquals(expectedSize, map.size());
	}

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

	@Test
	public void get() {
		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));
		Fruit value = map.get("Kiwifruit");
		System.out.println(value);
		assertEquals(expected, value.getQuantity(), 0);
	}

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

	@Test
	public void update() {
		double expected = 10d;
		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);

		Fruit put = map.put("Grape", new Fruit("Grape", 10, 1));
		assertEquals(1d, put.getQuantity(), 0);
		System.out.println(map);
		assertEquals(expected, map.get("Grape").getQuantity(), 0);
	}

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

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

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

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

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

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

心得分享

LinkedHashMap Functions in Java 實作集合框架的 Map 接口,繼承 HashMap,使用雙向鏈結的方式實作,保證新增時的順序,熟悉 LinkedHashMap Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言