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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
VitorharshArun Recent comment authors
newest oldest most voted
Vitor
Guest
Vitor

thanks mk

harsh
Guest
harsh

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

Arun
Guest
Arun

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
Guest
Arun

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