Java 8 – Conversion of Arrays to List

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

Conversion of Arrays to List :

  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:

1. Before Java 8 – Conversion of Arrays into List

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<String>

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

Conversion of String Arrays to List<String>

[James, Bond, Michael, Pups, Jackson, Bird]

2. Java 8 – Conversion of Arrays into List using Streams

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.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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