Java Array to List
In Java, Array and List are the two most important data structures. In this section, we will learn how to convert Java Array into a List. We have also created Java programs that convert Array into a List by using different Java methods.
Converting Array to List in Java
Java array is a collection of multiple values of the same data type. An array can contain objects and primitive types. It depends on the definition of the array. If an array definition is of primitive type, the values of the array store in the contagious memory location. If an array contains objects elements, elements stored in the heap segment.
In Java, a List is an interface that belongs to the Java Collections framework. It stores elements in the form of objects in an ordered manner and preserves the insertion order. It allows us to store duplicate values. The classes ArrayList, LinkedList, Vector and Stack implements the List interface.
Java provides five methods to convert Array into a List are as follows:
It is the simplest method to convert Java Array into a List. In this method first, we create an empty List and add all the elements of the array into the List. Let's see an example.
Using Arrays.asList() Method
It is the method of the Java Arrays class that belongs to java.util package. When we use the asList() method with the Collection.toArray() method, it works as a bridge between array-based and collection-based APIs.
The method parses an array as a parameter by which the list will be backed. It returns a serializable fixed-size list view of the specified array. Let's see an example.
Using Collections.addAll() Method
It is the method of the Java Collections class. it belongs to java.util package. The class provides a method named addAll(). We can use the method to convert Array into a List. It adds all the elements to the specified collection. We can specify elements either individually or in the form of an array. It works the same as c.addAll(Arrays.asList(elements)). It is a faster implementation than another implementation.
It parses two parameters:
It returns true if the collection changed as a result of the call. It throws the following exceptions:
Let's see an example.
Using Java 8 Stream API
Java 8 provides the Stream API to process the collections of objects. It is a sequence of methods that can be pipelined to produce the desired result. Remember that it does not change the original data structure. It provides the output based on the pipelined methods. We can attain Stream in a number of ways but in the following program, we have used Arrays.stream(Object) to attain the stream.
Collectors.toList() Method: The method returns a Collector that collects the input elements into a newly created List in an encounter method.
Where T is the type of element that we have specified. The method does not provide guarantees on type, mutability, thread-safety, and serializability.
Let's use the Stream API in a Java program and convert an array into a List.
Using Guava Lists.newArrayList()
It is the method of the Lists class that belong to com.google.common.collect package. The class provides a method newArrayList() that creates mutable empty ArrayList instance having the elements of the specified array.
Note: The newArrayList() method is available for Java 6 and earlier versions. In later versions it is deprecated. Instead of the above method, we use ArrayList constructor directly.