Table of Contents
ToggleJava Convert Array to List
陣列和 List 是最重要的兩種資料結構, Array 是同一資料類型的多個值的集合,可以包含物件和基本類別, List 是屬於 Java Collections 框架的接口,物件的形式有序存儲元素,並保留插入順序,允許存儲重複值,Java Array List 任務是將該 Array 轉換為 List ,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- array
| +- ArrayToListTest.java
單元測試
Java Array to List 提供陣列轉換成 List 等操作。
iteration
Java Array to List 使用迴圈轉換成 List 。
@Test
public void iteration() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = new ArrayList<>();
for (String e : array) {
list.add(e);
}
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
List<Integer> intList = new ArrayList<>();
for (int e : intArray) {
intList.add(e);
}
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = new ArrayList<>();
for (int e : integerArray) {
integerList.add(e);
}
System.out.println(intList);
assertEquals(expectedSize, intList.size());
}
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
Arrays_asList
Java Array to List 使用 Arrays.asList 轉換成 List。
@Test
public void Arrays_asList() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Arrays.asList(array);
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
Integer[] boxedArray = IntStream.of(intArray).boxed().toArray(Integer[]::new);
List<Integer> intList = Arrays.asList(boxedArray);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = Arrays.asList(integerArray);
System.out.println(integerList);
assertEquals(expectedSize, integerList.size());
}
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
Collections_addAll
Java Array to List 使用 Collections.addAll 轉換成 List。
@Test
public void Collections_addAll() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = new ArrayList<>();
Collections.addAll(list, array);
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
List<Integer> intList = new ArrayList<>();
Integer[] boxedArray = Arrays.stream(intArray).boxed().toArray(Integer[]::new);
Collections.addAll(intList, boxedArray);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = new ArrayList<>();
Collections.addAll(integerList, integerArray);
System.out.println(integerList);
assertEquals(expectedSize, integerList.size());
}
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
Arrays_stream
Java Array to List 使用 Arrays.stream 轉換成 List。
@Test
public void Arrays_stream() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Arrays.stream(array).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
list = Arrays.stream(array).collect(Collectors.toCollection(LinkedList::new));
System.out.println(list);
assertEquals(LinkedList.class, list.getClass());
}
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
Stream_of
Java Array to List 使用 Stream.of 轉換成 List。
@Test
public void Stream_of() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Stream.of(array).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
list = Stream.of(array).collect(Collectors.toCollection(LinkedList::new));
System.out.println(list);
assertEquals(LinkedList.class, list.getClass());
}
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
[Java 147, Spring boot 168, Junit 151, Bash 460, IT 484]
ArrayToListTest.java
Convert Array to List in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.array;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Test;
public class ArrayToListTest {
@Test
public void iteration() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = new ArrayList<>();
for (String e : array) {
list.add(e);
}
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
List<Integer> intList = new ArrayList<>();
for (int e : intArray) {
intList.add(e);
}
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = new ArrayList<>();
for (int e : integerArray) {
integerList.add(e);
}
System.out.println(intList);
assertEquals(expectedSize, intList.size());
}
@Test
public void Arrays_asList() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Arrays.asList(array);
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
Integer[] boxedArray = IntStream.of(intArray).boxed().toArray(Integer[]::new);
List<Integer> intList = Arrays.asList(boxedArray);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = Arrays.asList(integerArray);
System.out.println(integerList);
assertEquals(expectedSize, integerList.size());
}
@Test
public void Collections_addAll() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = new ArrayList<>();
Collections.addAll(list, array);
System.out.println(list);
assertEquals(expectedSize, list.size());
int[] intArray = { 147, 168, 151, 460, 484 };
List<Integer> intList = new ArrayList<>();
Integer[] boxedArray = Arrays.stream(intArray).boxed().toArray(Integer[]::new);
Collections.addAll(intList, boxedArray);
System.out.println(intList);
assertEquals(expectedSize, intList.size());
Integer[] integerArray = { 147, 168, 151, 460, 484 };
List<Integer> integerList = new ArrayList<>();
Collections.addAll(integerList, integerArray);
System.out.println(integerList);
assertEquals(expectedSize, integerList.size());
}
@Test
public void Arrays_stream() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Arrays.stream(array).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
list = Arrays.stream(array).collect(Collectors.toCollection(LinkedList::new));
System.out.println(list);
assertEquals(LinkedList.class, list.getClass());
}
@Test
public void Stream_of() {
int expectedSize = 5;
String[] array = { "Java 147", "Spring boot 168", "Junit 151", "Bash 460", "IT 484" };
List<String> list = Stream.of(array).collect(Collectors.toList());
System.out.println(list);
assertEquals(expectedSize, list.size());
list = Stream.of(array).collect(Collectors.toCollection(LinkedList::new));
System.out.println(list);
assertEquals(LinkedList.class, list.getClass());
}
}
心得分享
Array to List in Java 可以使用不同的方法將 Array 轉換為 List , Convert Array to List in Java 提供將 Array 實例轉換為 List 實例的多種方法。