Table of Contents
ToggleImmutable List in Java with Examples
介紹使用 JDK、Guava 以及 Apache Collections 4 來建立無法修改的集合,此集合是線程安全的,記憶效率很高, 由於是不可變的,因此可以毫無問題地傳遞給第三方程式庫,Immutable List in Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- list
| +- ImmutableListWithExamplesTest.java
單元測試
List Immutable Java 提供建立不可變的的集合。
Fruit
建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@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();
}
}
Arrays_asList
建立一個 List ,使用 Arrats asList 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void Arrays_asList() {
List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
assertThatCode(() -> list.add("Kiwifruit")).isInstanceOf(UnsupportedOperationException.class);
System.out.println(list);
assertThat(list).hasSize(3);
List<Integer> intList = Arrays.asList(Integer.MAX_VALUE, -1, 3);
assertThatCode(() -> intList.add(101)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(intList);
assertThat(intList).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
Collections_unmodifiableList
List Immutable Java 建立一個 List ,使用 Collections unmodifiableList 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void Collections_unmodifiableList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = Collections.unmodifiableList(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
Collections_singletonList
List Immutable Java 建立一個 List ,使用 Collections singletonList 初始化一個元素,只能包含一個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void Collections_singletonList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
List<Fruit> result = Collections.singletonList(apple);
Fruit banana = new Fruit("Banana", -1, 3);
assertThatCode(() -> result.add(banana)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(1);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}]
ImmutableList_of
List Immutable Java 建立一個 List ,使用 Guava ImmutableList of 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void ImmutableList_of() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = ImmutableList.of(apple, banana, cherry);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> list.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(list);
assertThat(list).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
ImmutableList_copyOf
List Immutable Java 建立一個 List ,使用 Guava ImmutableList copyOf 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void ImmutableList_copyOf() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ImmutableList.copyOf(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
ImmutableList_builder
Java Immutable List 建立一個 List ,使用 Guava ImmutableList builder 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void ImmutableList_builder() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ImmutableList.<Fruit>builder().addAll(list).build();
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
ListUtils_unmodifiableList
Java Immutable List 建立一個 List ,使用 Apache Collections4 套件中的 ListUtils unmodifiableList 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void ListUtils_unmodifiableList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ListUtils.unmodifiableList(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
ImmutableListWithExamplesTest.java
Java Immutable List 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.list;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import com.google.common.collect.ImmutableList;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class ImmutableListWithExamplesTest {
@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();
}
}
@Test
public void Arrays_asList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = Arrays.asList(apple, banana, cherry);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> list.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(list);
assertThat(list).hasSize(3);
}
@Test
public void Collections_unmodifiableList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = Collections.unmodifiableList(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
@Test
public void ImmutableList_of() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = ImmutableList.of(apple, banana, cherry);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> list.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(list);
assertThat(list).hasSize(3);
}
@Test
public void ImmutableList_copyOf() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ImmutableList.copyOf(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
@Test
public void ImmutableList_builder() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ImmutableList.<Fruit>builder().addAll(list).build();
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
@Test
public void ListUtils_unmodifiableList() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit banana = new Fruit("Banana", -1, 3);
Fruit cherry = new Fruit("Cherry", 3, 1);
List<Fruit> list = new ArrayList<>();
list.add(apple);
list.add(banana);
list.add(cherry);
List<Fruit> result = ListUtils.unmodifiableList(list);
Fruit kiwifruit = new Fruit("Kiwifruit", 101, 2);
assertThatCode(() -> result.add(kiwifruit)).isInstanceOf(UnsupportedOperationException.class);
System.out.println(result);
assertThat(result).hasSize(3);
}
}
心得分享
Java Immutable List Example 提供了幾種不可變集合常見方法的操作範例,在應用上相當廣泛,熟悉 Java Immutable List 這些方法的操作,像是: Arrays.asList 、 Collections.unmodifiableList 、 ImmutableList.of 、 ListUtils.unmodifiableList 等方法,更方便簡易地建立集合。