1. Introduction
In this tutorial, you will learn how to convert an Array to List in java.
An array is a group of same type variables that hold a common name. Array values are accessed by an index that starts from 0. Arrays can hold primitive types and objects based on how the array is defined. If the array is created to store the primitive values then these are stored in the contiguous memory locations whereas objects are stored in the heap memory.
Java.util.List is sub-interface to the Collection interface. List is intended to store the values based on the index and accessed through index only. But, duplicate values can be stored in the list. List interfaces implementations are AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector.
List objects are accessed through the iterator() and listiterator() methods.
Let us dive into our core article to convert Array to List and this can be done in the following 4 ways.
- Native Generics Apparoach
- Arrays.asList()
- Collections.addAll
- Java 8 Stream API - Arrays.stream().collect()
- Java 8 Stream boxed() for primitive arrays
Read more on How to convert Sting to Arraylist using Arrays.asList().
Input 1 : Array -> {"java", "program", "to.com", "is a ", "java portal"} Output 1 : List -> {"java", "program", "to.com", "is a ", "java portal"} Input 2 : Array -> {10, 20, 30, 40, 50} Output 2 : List -> {10, 20, 30, 40, 50}
All Examples :
// 1. Example on Arrays.asList() List<T> outputList = Arrays.asList(array); // 2. Example on Collections.addAll() Collections.addAll(outputList, array); // 3. Example on Arrays.stream().collect() List<T> outputList = Arrays.stream(array).collect(Collectors.toList()); // 4. Example on boxed() method for primitive int array List<Integer> outputList = Arrays.stream(array).boxed().collect(Collectors.toList());
2. Convert List to Array using Native Generic Approach
package com.javaprogramto.java8.arraytolist; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayToListExample { public static void main(String[] args) { String[] intArray = {"10", "20", "30", "40", "50"}; System.out.println("Array : " + Arrays.toString(intArray)); List<String> list = convertArrayToList(intArray); System.out.println("COnverted ArrayList : " + list); } private static <T> List<T> convertArrayToList(T[] array) { List<T> outputList = new ArrayList<T>(); for (T t : array) { outputList.add(t); } return outputList; } }Output:
Array : [10, 20, 30, 40, 50] COnverted ArrayList : [10, 20, 30, 40, 50]
3. Convert List to Array Using Arrays.asList()
Arrays class has a utility static method asList() which takes an array as input and returns a list object with array values.
package com.javaprogramto.java8.arraytolist; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayToListAsListExample { public static void main(String[] args) { String[] intArray = {"java", "program", "to.com", "is a ", "java portal"}; System.out.println("Array : " + Arrays.toString(intArray)); List<String> list = convertArrayToList(intArray); System.out.println("Converted ArrayList : " + list); } private static <T> List<T> convertArrayToList(T[] array) { List<T> outputList = new ArrayList<T>(); outputList = Arrays.asList(array); return outputList; } }
Output:
Array : [java, program, to.com, is a , java portal] Converted ArrayList : [java, program, to.com, is a , java portal]
4. Convert List to Array Using Collections.addAll()
Collections class has a utility static method addAll() which takes two parameters. The first argument is the Output List and the second argument is the array of values. This method converts the second argument array to List. It populates the values from array to the given output list.
package com.javaprogramto.java8.arraytolist; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ArrayToListCollectionsAddAllExample { public static void main(String[] args) { // Example using Collections.addAll(outputList, array); String[] intArray = {"java", "program", "to.com", "is a ", "java portal"}; System.out.println("Array : " + Arrays.toString(intArray)); List<String> list = convertArrayToList(intArray); System.out.println("Converted ArrayList : " + list); } private static <T> List<T> convertArrayToList(T[] array) { List<T> outputList = new ArrayList<T>(); Collections.addAll(outputList, array); return outputList; } }
Output:
Array : [java, program, to.com, is a , java portal] Converted ArrayList : [java, program, to.com, is a , java portal]
5. Java 8 Convert Array to List using Arrays.stream().collect()
Java 8 Stream API has added with a stream() method in the Arrays class to provide arrays support to Streams.
First, Convert the array into Stream by using Arrays.stream()
Second, Convert Stream into list using Collectors.toList() method.
Finally, Collect the converted stream into list using stream.collect() method.
package com.javaprogramto.java8.arraytolist; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ArrayToListJava8Stream { public static void main(String[] args) { // Example using Collections.addAll(outputList, array); String[] intArray = {"java", "program", "to", "convert", "array to list"}; System.out.println("Array : " + Arrays.toString(intArray)); List<String> list = convertArrayToList(intArray); System.out.println("Converted ArrayList : " + list); } private static <T> List<T> convertArrayToList(T[] array) { // convert array to stream Stream<T> stream = Arrays.stream(array); // collecting converted stream into list List<T> outputList = stream.collect(Collectors.toList()); return outputList; } }
Output:
Array : [java, program, to, convert, array to list] Converted ArrayList : [java, program, to, convert, array to list]
6. Java 8 Convert Primitive Array to List using boxed() method
Stream api is added with boxed() method to work with primitive collections such as int[], float[] and double[] arrays.
boxed() method converts primitive values to its wrapper objects.
package com.javaprogramto.java8.arraytolist; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ArrayToListJava8Boxed { public static void main(String[] args) { // Example using stream.boxed() int[] intArray = {1, 2, 3, 4, 5, 6, 6}; System.out.println("Array : " + Arrays.toString(intArray)); List<Integer> list = convertPrimitiveArrayToList(intArray); System.out.println("Converted ArrayList : " + list); } private static List<Integer> convertPrimitiveArrayToList(int[] array) { // convert int array to Integer stream with boxed() method Stream<Integer> stream = Arrays.stream(array).boxed(); // collecting converted stream into list List<Integer> outputList = stream.collect(Collectors.toList()); return outputList; } }Output:
Array : [1, 2, 3, 4, 5, 6, 6] Converted ArrayList : [1, 2, 3, 4, 5, 6, 6]
7. Conclusion
In this article, you've seen all possible ways in the Java traditional and java 8 stream api.
As usual, all examples shown are on GitHub.
Arrays.asList()
Collections.addAll()
Arrays.stream()
Stream.boxed()
Ref
Stackoverflow
Arrays.asList()
Collections.addAll()
Arrays.stream()
Stream.boxed()
Ref
Stackoverflow
No comments:
Post a Comment
Please do not add any spam links in the comments section.