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]
thanks mk
this is gud.but how to convert int[] into list and then sort the list according to ascending order
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.
It must be Object[] not primitive{int,float,long,double,char} type array.