How to convert Array to List in Java

Sometime you need to convert an array variable to List or Collection for some iterative (looping) process. Actually Java come with a handy function to achieve this – Arrays.asList. See example below :

import java.util.Arrays;
import java.util.List;
 
public class ArrayToList {
  public static void main(String[] argv) {
 
	  String sArray[] = new String []{"Array 1", "Array 2", "Array 3"};
 
	  //convert array to list
	  List lList = Arrays.asList(sArray);
 
	  System.out.println(lList);
 
  }
}
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • harsh

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

  • 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

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