Efficiently Convert an Array to a List in Java

There are many ways to convert an array into a list in Java. The following are some of the examples of how you can efficiently convert an array to a list in Java:

Array of String Items to List


// sample array of string items
String[] strArray = { "Science", "English", "Geography", "Economics", "Computer" };
// converting to List
List<String> bookList = Arrays.asList(strArray);

Array of int Items to List

Below Java 8:


// sample array of int items
int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// converting to list
List<Integer> intList = new ArrayList<Integer>();
for (int i : intArray) {
	intList.add(i);
}

In Java 8 and above:


// sample array of int items
int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// converting to list example 1
List<Integer> intList1 = Arrays.stream(intArray).boxed().collect(Collectors.<Integer>toList());
		
// converting to list example 2
List<Integer> intList2 =Arrays.stream(intArray).boxed().toList();