Conversion of Arrays to List in Java 8

In this article, we will discuss how to convert Arrays into List in Java 8 using Streams class

 

Solution:

  1. Until Java 1.7 version, we can use asList(); method of Arrays
  2. In Java 1.8 version, conversion is very simple using Streams class

 

Read below articles on conversion of Arrays to List:

 

Solution 1: Conversion of Arrays into List until 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 using sysout
		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 using sysout
		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]

 

Solution 2: Conversion of Arrays into List using Streams class in Java 1.8 version

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 class\n");

		// simply print to console using sysout
		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 class\n");

		// simply print to console using sysout
		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 class\n");

		// simply print to console using sysout
		System.out.println(strList);
	}
}

Output:

Conversion of primitive int Arrays to List<Integer> using Java 8 Streams class

[1, 2, 3, 4]


Conversion of Integer Arrays to List<Integer> using Java 8 Streams class

[31, 83, 53, 97, 29, 7, 13, 47, 79]


Conversion of String Arrays to List<String> using Java 8 Streams class

[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.

 

References:

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Conversion of Map to List in Java
Various ways to remove duplicate elements from Arrays in Java