How to convert Array to List in Java

Java example to show you how to convert a Array to a List

ArrayToList.java

package com.mkyong;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToList {

	public static void main(String[] argv) {

		String sArray[] = new String[] { "A", "B", "C" };

		// convert array to list #1
		List<String> list = Arrays.asList(sArray);

		System.out.println(list);

		// convert array to list #2
		List<String> list2 = new ArrayList<String>(Arrays.asList(sArray));

		System.out.println(list2);

		int iArray[] = new int[] { 1, 2, 3 };

		// Java 8, convert array to List, primitive int[] to List<Integer>
		List<Integer> list3 = Arrays.stream(iArray).boxed().collect(Collectors.toList());

		System.out.println(list3);
	}

}

Output


[A, B, C]
[A, B, C]
[1, 2, 3]

References

  1. Java – How to convert a primitive Array to List

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Vitor
12 years ago

thanks mk

harsh
13 years ago

this is gud.but how to convert int[] into list and then sort the list according to ascending order

Arun
14 years ago

This is not true to all, you can’t convert byte array to list nor do char [], javadoc speaks out.So, the topic is not perfectly correct.

Arun
14 years ago
Reply to  Arun

It must be Object[] not primitive{int,float,long,double,char} type array.