你好朋友,
在本教程中,我们将学习将数组转换为List的各种方法。
![](https://i-blog.csdnimg.cn/blog_migrate/8dae3c4e9dad2b0bbeccf4e243d66dd4.png)
package com.blogspot.javasolutionsguide;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
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;
public class ArrayToList {
public static void main(String[] args) {
//Before Java 8
System.out.println("Before JAVA 8........................................");
// 1.
String[] names = new String[] {"Gaurav", "Sachin", "Yuvraj"};
List<String> nameList = Arrays.asList(names);
System.out.println("using Arrays.asList: " + nameList);
//2
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println("Inline primitive array to Integer List using Arrays.asList(): "
+ list);
//Java 8
System.out.println("JAVA 8 ................................................");
// 3. primitive array
int intArray[] = {1, 2, 3};
List<Integer> integerList1 = Arrays.stream(intArray).boxed()
.collect(Collectors.toList());
System.out.println("Primitive int array to Integer List using Array.stream(): "
+ integerList1);
// 4. primitive array with IntStream
List<Integer> integerList2 = IntStream.of(intArray).boxed()
.collect(Collectors.toList());
System.out.println("Primitive int array to Integer List using IntStream.of(): "
+ integerList2);
// 5. Object type array to Integer List
Integer integerArray[] = {1, 2, 3};
List<Integer> integerList3 = Arrays.stream(integerArray)
.collect(Collectors.toList());
System.out.println("Object type array to Integer List using Arrays.stream: "
+ integerList3);
// 6. Integer Array to ArrayList
ArrayList<Integer> arrayList = Arrays.stream(integerArray)
.collect(Collectors.toCollection(ArrayList::new));
System.out.println("Integer type array to Integer ArrayList using Arrays.stream: "
+ arrayList);
// 7. Integer Array to LinkedList
LinkedList<Integer> linkedList = Stream.of(integerArray)
.collect(Collectors.toCollection(LinkedList::new));
System.out.println("Integer type array to Integer LinkedList using Arrays.stream: "
+ linkedList);
// 8. Integer Array to Immutable List
List<Integer> immutableList = Collections.unmodifiableList(Arrays.asList(integerArray));
System.out.println("Integer type array to Immutable List using Arrays.stream: "
+ immutableList);
//9. JAVA 9
System.out.println("JAVA 9.........................................................");
String[] playerNames = {"Sachin", "Dhoni", "Yuvraj"};
List<String> players = List.of(playerNames);
System.out.println("Array to List using Java 9 List.of() :" + players);
//10. JAVA 10
System.out.println("JAVA 10.........................................................");
List<Integer> integerList = List.copyOf(Arrays.asList(integerArray));
System.out.println("Array to List using Java 10 List.copyOf() :" + integerList);
//11. Apache Commons
System.out.println("Apache Commons...................................................");
List<Integer> targetList = new ArrayList<>(6);
CollectionUtils.addAll(targetList, integerArray);
System.out.println("Array to List using Apache Common CollectionUtils.addAll() :"
+ integerList);
//12 Google Guava
System.out.println("Google Guava......................................................");
Integer[] sourceArray = {1, 2, 3};
List<Integer> targetList1 = Lists.newArrayList(sourceArray);
System.out.println("Array to List using Google Guava Lists.newArrayList:"
+ integerList);
}
}
External Dependency Used :
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
本教程的所有代码都可以在GitHub中找到 。 因此,在本教程中,我们学习了将数组转换为Java中的List的各种方法,这是本教程的全部内容。请订阅博客以获取更多此类教程。
翻译自: https://www.javacodegeeks.com/2020/05/how-to-convert-array-to-list-in-java.html