Javatpoint Logo
Javatpoint Logo

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 Array to List

Java provides five methods to convert Array into a List are as follows:

  • Native Method
  • Using Arrays.asList() Method
  • Using Collections.addAll() Method
  • Using Java 8 Stream API
  • Using Guava Lists.newArrayList() Method

Native Method

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.

ArrayToListExample1.java

Output:

Java Array to List

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.

Syntax:

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.

ArrayToListExample2.java

Output:

Java Array to List

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.

Syntax:

It parses two parameters:

  • c: It is a collection in which elements are to be added.
  • elements: The elements are to be inserted into c.

It returns true if the collection changed as a result of the call. It throws the following exceptions:

  • If the parameter c does not support the add operation, it throws UnsupportedOperationException.
  • If the specified array elements contain one or more null values and c does not allow null elements, it throws NullPointerException.
  • If any array element prevents it from being added to the parameter c it throws IllegalPointerException.

Let's see an example.

ArrayToListExample3.java

Output:

Java Array to List

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.

Syntax:

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.

ArrayToListExample4.java

Output:

Java Array to 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.

Syntax:

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.

ArrayToListExample5.java

Output:

Java Array to List
Next TopicJIT in Java




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA