In this article, we will discuss how to convert Arrays into List in Java 8 using Streams class
Solution (version-wise):
- Until Java 1.7 version, we can use asList(); method of Arrays
- In Java 1.8 version, conversion is very simple using Streams class
Read below articles on conversion of Arrays to List:
1. Conversion of Arrays into List in Java 1.7 version
Method signature:
public static List asList(Object[] oArray);
ConvertArraysIntoList.java
package in.bench.resources.java.collections; import java.util.Arrays; import java.util.List; public class ConvertArraysIntoList { public static void main(String[] args) { // original Arrays Integer[] intArrays = {31, 83, 53, 97, 29, 7, 13, 47, 79}; String[] strArrays = { "James", "Bond", "Michael", "Pups", "Jackson", "Bird" }; // conversion of Integer Arrays to List List<Integer> intList = Arrays.asList(intArrays); System.out.println("Conversion of Integer Arrays" + " to List<Integer>\n"); // simply print to console System.out.println(intList); // conversion of String Arrays to List List<String> strList = Arrays.asList(strArrays); System.out.println("\n\nConversion of String Arrays" + " to List<String>\n"); // simply print to console System.out.println(strList); } }
Output:
Conversion of Integer Arrays to List<Integer> [31, 83, 53, 97, 29, 7, 13, 47, 79] Conversion of String Arrays to List<String> [James, Bond, Michael, Pups, Jackson, Bird]
2. Conversion of Arrays into List using Streams in Java 1.8 version
Method signature:
// conversion of Arrays to List in Java 1.8 version List<T> refList = Arrays.stream(arrObj).boxed() .collect(Collectors.toList());
ConvertListIntoArraysInJava8.java
package in.bench.resources.java.collections; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ConvertListIntoArraysInJava8 { public static void main(String[] args) { // original Arrays int[] primInt = {1, 2, 3, 4}; Integer[] intArrays = {31, 83, 53, 97, 29, 7, 13, 47, 79}; String[] strArrays = { "James", "Bond", "Michael", "Pups", "Jackson", "Bird" }; // Example 1: conversion of primitive int Arrays to List List<Integer> primitiveIntList = Arrays.stream(primInt) .boxed().collect(Collectors.toList()); System.out.println("Conversion of" + " primitive int Arrays to List<Integer>" + " using Java 8 Streams \n"); // simply print to console System.out.println(primitiveIntList); // Example 2: conversion of Integer Arrays to List List<Integer> intList = Arrays.stream(intArrays) .collect(Collectors.toList()); System.out.println("\n\nConversion of" + " Integer Arrays to List<Integer> " + "using Java 8 Streams \n"); // simply print to console System.out.println(intList); // Example 3: conversion of String Arrays to List List<String> strList = Arrays.stream(strArrays) .collect(Collectors.toList()); System.out.println("\n\nConversion of" + " String Arrays to List<String> " + "using Java 8 Streams \n"); // simply print to console System.out.println(strList); } }
Output:
Conversion of primitive int Arrays to List<Integer> using Java 8 Streams [1, 2, 3, 4] Conversion of Integer Arrays to List<Integer> using Java 8 Streams [31, 83, 53, 97, 29, 7, 13, 47, 79] Conversion of String Arrays to List<String> using Java 8 Streams [James, Bond, Michael, Pups, Jackson, Bird]
Few important points about conversion of Arrays to List:
- This conversion, only provides the List view of Arrays elements; Not altogether separate list
- Performing any operation on elements will reflect on both Arrays & List
- But modification operations like adding/removing will throw UnSupportedOperationException
- If we take generic List<Object> or simply List while conversion, then after conversion adding any other type other than Arrays type will throw ArrayStoreException
Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Read Also:
- Conversion of List to Map in Java 8
- Conversion of List to Map (until Java 1.7 version)
- Conversion of Map to List in Java 8
- Conversion of Map to List (until Java 1.7 version)
- Conversion of List to Arrays
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!