Table of Contents
ToggleJava ArrayList Initialization
實作 List 接口,初始化集合可以存入重複元素,介紹常見的 constructor 、 immutableList 、 stream 等方法,了解初始化集合的不同操作和方法, ArrayList Java Initialization 初始化集合,會隨著元素增加或移除,大小自動增長或縮小,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- list
| +- arraylist
| +- ArrayListInitializationTest.java
單元測試
Initialization of ArrayList 提供初始化操作列表中的元素。
constructor
建立一個 ArrayList ,使用建構子,初始化三個元素。
@Test
public void constructor() {
int expectedSize = 3;
List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
List<String> newList = new ArrayList<>(list);
System.out.println(newList);
assertEquals(expectedSize, newList.size());
List<Integer> intList = Arrays.asList(1, 2, 3);
List<Integer> newIntList = new ArrayList<>(intList);
System.out.println(newIntList);
assertEquals(expectedSize, newIntList.size());
}
[Apple, Banana, Cherry]
[1, 2, 3]
Stream_of
Initialization of ArrayList 建立一個 ArrayList ,使用 Stream of 初始化三個元素。
@Test
public void Stream_of() {
int expectedSize = 3;
List<String> list = Stream.of("Apple", "Banana", "Cherry").collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = Stream.of(1, 2, 3).collect(Collectors.toList());
System.out.println(intList);
assertEquals(expectedSize, intList.size());
List<Integer> intArrayList = Stream.of(new int[] { 1, 2, 3 }).flatMapToInt(e -> Arrays.stream(e)).boxed()
.collect(Collectors.toList());
System.out.println(intArrayList);
assertEquals(expectedSize, intArrayList.size());
}
[Apple, Banana, Cherry]
[1, 2, 3]
Collections_addAll
Initialization of ArrayList 建立一個 ArrayList ,使用 Collections addAll 初始化三個元素。
@Test
public void Collections_addAll() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
Collections.addAll(list, "Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = new ArrayList<>();
Collections.addAll(intList, 1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, list.size());
}
[Apple, Banana, Cherry]
[1, 2, 3]
Collections_unmodifiableList
Initialization of ArrayList 建立一個 List ,使用 Collections unmodifiableList 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void Collections_unmodifiableList() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> result = Collections.unmodifiableList(list);
System.out.println(result);
assertThatCode(() -> result.add("Grape")).isInstanceOf(UnsupportedOperationException.class);
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
List<Integer> intResult = Collections.unmodifiableList(intList);
System.out.println(intResult);
assertThatCode(() -> intResult.add(Integer.MAX_VALUE)).isInstanceOf(UnsupportedOperationException.class);
}
[Apple, Banana, Cherry]
[1, 2, 3]
ImmutableList_of
Initializing ArrayList in Java 建立一個 List ,使用 Guava ImmutableList of 初始化三個元素,為不可變的集合,若操作新增、刪除等方法,會拋出例外。
@Test
public void ImmutableList_of() {
int expectedSize = 3;
List<String> list = ImmutableList.of("Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
assertThatCode(() -> list.add("Grape")).isInstanceOf(UnsupportedOperationException.class);
List<Integer> intList = ImmutableList.of(1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
assertThatCode(() -> intList.add(Integer.MAX_VALUE)).isInstanceOf(UnsupportedOperationException.class);
}
[Apple, Banana, Cherry]
[1, 2, 3]
Lists_newArrayList
Initializing ArrayList in Java 建立一個 ArrayList ,使用 Guava Lists newArrayList 初始化三個元素。
@Test
public void Lists_newArrayList() {
int expectedSize = 3;
List<String> list = Lists.newArrayList("Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = Lists.newArrayList(1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
}
[Apple, Banana, Cherry]
[1, 2, 3]
ArrayListInitializationTest.java
Initializing ArrayList in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.list.arraylist;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class ArrayListInitializationTest {
@Test
public void constructor() {
int expectedSize = 3;
List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
List<String> newList = new ArrayList<>(list);
System.out.println(newList);
assertEquals(expectedSize, newList.size());
List<Integer> intList = Arrays.asList(1, 2, 3);
List<Integer> newIntList = new ArrayList<>(intList);
System.out.println(newIntList);
assertEquals(expectedSize, newIntList.size());
}
@Test
public void Stream_of() {
int expectedSize = 3;
List<String> list = Stream.of("Apple", "Banana", "Cherry").collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = Stream.of(1, 2, 3).collect(Collectors.toList());
System.out.println(intList);
assertEquals(expectedSize, intList.size());
List<Integer> intArrayList = Stream.of(new int[] { 1, 2, 3 }).flatMapToInt(e -> Arrays.stream(e)).boxed()
.collect(Collectors.toList());
System.out.println(intArrayList);
assertEquals(expectedSize, intArrayList.size());
}
@Test
public void Collections_addAll() {
int expectedSize = 3;
List<String> list = new ArrayList<>();
Collections.addAll(list, "Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = new ArrayList<>();
Collections.addAll(intList, 1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, list.size());
}
@Test
public void Collections_unmodifiableList() {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
List<String> result = Collections.unmodifiableList(list);
System.out.println(result);
assertThatCode(() -> result.add("Grape")).isInstanceOf(UnsupportedOperationException.class);
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
List<Integer> intResult = Collections.unmodifiableList(intList);
System.out.println(intResult);
assertThatCode(() -> intResult.add(Integer.MAX_VALUE)).isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void ImmutableList_of() {
int expectedSize = 3;
List<String> list = ImmutableList.of("Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
assertThatCode(() -> list.add("Grape")).isInstanceOf(UnsupportedOperationException.class);
List<Integer> intList = ImmutableList.of(1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
assertThatCode(() -> intList.add(Integer.MAX_VALUE)).isInstanceOf(UnsupportedOperationException.class);
}
@Test
public void Lists_newArrayList() {
int expectedSize = 3;
List<String> list = Lists.newArrayList("Apple", "Banana", "Cherry");
System.out.println(list);
assertEquals(expectedSize, list.size());
List<Integer> intList = Lists.newArrayList(1, 2, 3);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
}
}
心得分享
Java Initializing ArrayList 初始化集合元素,會依新增順序保持排序,以陣列索引為基礎,是一個非同步的操作,允許隨機訪問, Initializing ArrayList in Java 提供了幾種 ArrayList 初始化的操作範例,使用單元測試驗證產出結果。