Convert array to list in Java
In this post, you will learn how to convert an array to a list using the Java programming language.
Arrays and lists are the most commonly used data structures in Java. An array is a container that can hold a fixed number of items, and these items should be of the same type. Once an array is created, we cannot change its size. In Java, an array can hold primitives or objects.
A list is a sequence of indexed and ordered values. Java List is an interface that extends the Collection interface. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. Java List provides control over the position where you can insert an element. In the development process, we may come across a situation where we need to convert an array to a list.
Converting array to list using native method
In the given Java program, we have used the naïve approach to convert an array to a list. First, we create an empty list and iterate through the items in the array. In each iteration, we add the element to the list.
// Java Program to convert
// array to list
import java.util.*;
import java.util.stream.*;
class ArrayToList {
// Generic function to convert an Array to List
public static <T> List<T> convertArrayToList(T array[])
{
// Create an empty List
List<T> list = new ArrayList<>();
// Iterate through the array
for (T t : array) {
// Add each element into the list
list.add(t);
}
// Return the converted List
return list;
}
public static void main(String args[])
{
// Create an Array
String array[]
= { "apple","orange","guava","orange","kiwi" };
// Print the Array
System.out.println("Array: "
+ Arrays.toString(array));
// Convert the Array to List
List<String> list = convertArrayToList(array);
// Print the List
System.out.println("List: " + list);
}
}
Output of the above code:
Array: [apple, orange, guava, orange, kiwi]
List: [apple, orange, guava, orange, kiwi]
Converting array to list using Arrays.asList() method
The java.util.Arrays.asList returns a fixed-size list that is​ backed by the specified array. This method acts as a bridge between array-based and collection-based APIs. Here, we use the Arrays.asList() method of Java to convert an array to a list.
import java.util.*;
public class ConvertArrayToList
{
public static void main(String args[])
{
//creating an array to be converted
String arr[] = {"Apple","Orange","Mango","Guava","Kiwi" };
//print array before conversion
System.out.println("Array: "+ Arrays.toString(arr));
//converts Array into List using generic method
List<String> list = ArrayToListConversion(arr);
//prints the List
System.out.println("List: " + list);
}
// creating a generic function
// to converts the Array into List
public static <T> List<T> ArrayToListConversion(T arr[])
{
// invoking the asList() method
// passing the array to be converted
List<T> list = Arrays.asList(arr);
//returns the list
return list;
}
}
Converting array to list using Collections.addAll() method
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in the java.util package. It consists entirely of static methods which are used to operate on collections. In the given Java program, we have used the addAll() method of collections to convert an array to a list.
import java.util.*;
public class ConvertArrayToList
{
public static void main(String args[])
{
// Initializing an array
String arr[] = {"Pineapple","Jackfruit","Orange","Mango","Pear"};
// print the Array
System.out.println("Array: "+ Arrays.toString(arr));
// calling method
List<String> list = ArrayToListConversion(arr);
// print the List
System.out.println("List: " + list);
}
// Creating a generic function
// to convert the array into list
public static <T> List<T> ArrayToListConversion(T arr[])
{
// Creating the constructor of the List class
List<T> list = new ArrayList<>();
//the addAll() method adds Array to the List
Collections.addAll(list, arr);
//returns the list
return list;
}
}
Output of the above code:
Array: [Pineapple, Jackfruit, Orange, Mango, Pear]
List: [Pineapple, Jackfruit, Orange, Mango, Pear]
Converting array to list using Java 8 Stream API
Java 8 introduces the Stream API. The Stream API provides many methods which can be pipelined to produce the desired result. We can use the Collectors.toList() method of the Steam API to convert an array to a list. The method returns a Collector that collects the input elements into a newly created list in an encounter method. The Collectors.toList() method returns a Collector that collects the input elements into a newly created List in an encounter method.
// Java Program to convert
// Array to List in Java 8
import java.util.*;
import java.util.stream.*;
class ConvertArrayToList {
// Generic function to convert array to list
public static List convertArrayToList(T array[])
{
// create a list from the Array
return Arrays.stream(array).collect(
Collectors.toList());
}
public static void main(String args[])
{
// Initialize an Array
String arr[]
= { "Peas","Onion","Potato","Tomato","Beans" };
// Print the Array
System.out.println("Array: "
+ Arrays.toString(arr));
// Convert the Array to List
List list = convertArrayToList(arr);
// Print the List
System.out.println("List: " + list);
}
}
Output of the above code:
Array: [Peas, Onion, Potato, Tomato, Beans]
List: [Peas, Onion, Potato, Tomato, Beans]
Converting array to list using the Guava Lists.newArrayList() method
Guava is a set of core Java libraries from Google that includes new collection types, immutable collections, a graph library, and much more. The Lists.newArrayList() is a method of the Lists class that belongs to the com.google.common.collect package. In the given Java program, we create an empty list and add the array into the list by passing it as the parameter to the Lists.newArrayList() method.
// Java Program to convert
// array to list
import static com.google.common.collect.Lists.*;
import java.util.*;
public class ArrayToList
{
public static void main(String args[])
{
//Initializing array
String arr[] = { "One","Two","Three","Four","Five"};
// Array before conversion
System.out.println("Array: "+ Arrays.toString(arr));
//convert the Array to List
List list = ArrayToListConversion(arr);
System.out.println("List: " + list);
}
// Creating a generic function
// to converts the Array into List
public static List ArrayToListConversion(T arr[])
{
//creates a List from the specified Array
return Lists.newArrayList(arr);
}
}
Output of the above code:
Array: [One, Two, Three, Four, Five]
List: [One, Two, Three, Four, Five]
Related Articles
Java enumSort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java