Java Immutable Map - Java 147

Java Immutable Map – Java 147

Java Immutable Map

是一種不可變的 Map 類別,集合建立後,元素是固定的或不變的,也就是說集合是唯讀的,如果操作新增、刪除和更新集合中的元素,則會拋出 UnsupportedOperationException 的例外,同時也不允許 null 元素, 如果試圖建立具有 null 元素的 ImmutableMap,則會拋出 NullPointerException 的例外, Immutable Java Map 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

Immutable Map Java 提供建立不可變的的集合。

Collections_unmodifiableMap

Immutable Map Java 建立一個 Map ,使用 Collections unmodifiableMap 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void Collections_unmodifiableMap() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = Collections.unmodifiableMap(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}
{Grape=2147483647, Kiwifruit=-1, Lemon=3}

Collections_singletonMap

Immutable Map Java 建立一個 Map ,使用 Collections singletonMap 初始化一個元素,只能包含一個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void Collections_singletonMap() {
		Map<String, Integer> result = Collections.singletonMap("Grape", Integer.MAX_VALUE);
		assertThatCode(() -> result.put("Kiwifruit", -1)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(1);
	}
{Grape=2147483647}

ImmutableMap_of

Immutable Map Java 建立一個 Map ,使用 Guava ImmutableMap of 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void ImmutableMap_of() {
		Map<String, Integer> map = ImmutableMap.of("Grape", Integer.MAX_VALUE, "Kiwifruit", -1, "Lemon", 3);
		assertThatCode(() -> map.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(map);
		assertThat(map).hasSize(3);
	}
{Grape=2147483647, Kiwifruit=-1, Lemon=3}

ImmutableList_copyOf

Immutable Map Java 建立一個 Map ,使用 Guava ImmutableMap copyOf 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void ImmutableMap_copyOf() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = ImmutableMap.copyOf(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}
{Grape=2147483647, Kiwifruit=-1, Lemon=3}

ImmutableMap_builder

Java Map Immutable 建立一個 Map ,使用 Guava ImmutableMap builder 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void ImmutableMap_builder() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = ImmutableMap.<String, Integer>builder().putAll(map).build();
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}
{Grape=2147483647, Kiwifruit=-1, Lemon=3}

MapUtils_unmodifiableMap

Java Map Immutable 建立一個 Map ,使用 Apache Collections4 套件中的 MapUtils unmodifiableMap 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。

	@Test
	public void MapUtils_unmodifiableMap() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = MapUtils.unmodifiableMap(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}
{Grape=2147483647, Kiwifruit=-1, Lemon=3}

ImmutableMapTest.java

Java Map Immutable 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.junit.Test;

import com.google.common.collect.ImmutableMap;

public class ImmutableMapTest {

	@Test
	public void Collections_unmodifiableMap() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = Collections.unmodifiableMap(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}

	@Test
	public void Collections_singletonMap() {
		Map<String, Integer> result = Collections.singletonMap("Grape", Integer.MAX_VALUE);
		assertThatCode(() -> result.put("Kiwifruit", -1)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(1);
	}

	@Test
	public void ImmutableMap_of() {
		Map<String, Integer> map = ImmutableMap.of("Grape", Integer.MAX_VALUE, "Kiwifruit", -1, "Lemon", 3);
		assertThatCode(() -> map.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(map);
		assertThat(map).hasSize(3);
	}

	@Test
	public void ImmutableMap_copyOf() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = ImmutableMap.copyOf(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}

	@Test
	public void ImmutableMap_builder() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = ImmutableMap.<String, Integer>builder().putAll(map).build();
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}

	@Test
	public void MapUtils_unmodifiableMap() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("Grape", Integer.MAX_VALUE);
		map.put("Kiwifruit", -1);
		map.put("Lemon", 3);
		Map<String, Integer> result = MapUtils.unmodifiableMap(map);
		assertThatCode(() -> result.put("Papaya", 101)).isInstanceOf(UnsupportedOperationException.class);
		System.out.println(result);
		assertThat(result).hasSize(3);
	}
}

心得分享

Map Immutable in Java 無法在不可修改的 Map 中新增或刪除新元素,但可以使用物件參考修改儲存在其中的元素,Java Map Immutable 使用 JDK、Guava 以及 Apache Collections 4 來建立不可變的集合,提供了範例參考。

發佈留言