Convert List to Array in Java

Read Time:5 Minute, 42 Second

Table of Contents

In Java, there are numerous instances where you must transform a list into an array. For example, a function only accepts an array as a parameter instead of a collection.

The List interface is responsible for tracking the collection’s order. Further, the latter is a Collection’s child interface. It’s a sorted collection of objects that can store duplicate values. In addition, the list supports positional access and insertion of members because the insertion order is preserved. Now that we’ve been given a list of strings, whether a LinkedList or an ArrayList, our goal is to convert it to an array of strings in Java utilizing various methods.

Methods

  • The toArray() method is used
  • The get() method is used
  • Streams introduction in Java 8

toArray() method

This method returns an Object[] array whose elements are in the same order as the list’s elements. When performing some actions, casting is used to indicate the type of each element.

It’s another technique to make an array out of a list. We can easily convert a list to an array by utilizing the toArray() method. In addition, the toArray() method returns an array containing all of the list’s elements. The entries in the returned array are in the same order as those in the list.

SUGGESTED READ

The toArray() method is available on the List object. This function takes an empty array as a parameter and converts the current list to an array before inserting it into the specified array. This approach can be divided into two categories.

  • toArray()
  • toArray(T[] arr)

The toArray() function of the List interface has the following syntax:

The toArray() method takes an array as an input or none and produces an array containing all the list’s members. Let’s look at another example of converting a list to an array to see how you might use the toArray() method of the list. To convert a List object to an array, use the following code.

  • Make a List object out of it.
  • Toss in some extras(elements).
  • Make an empty array the same size as the ArrayList you just made.
  • Bypass the above-created array as an input to the toArray() function when converting the list to an array.
  • The final step is printing the array’s contents.

The examples below demonstrate converting a List of Strings and a List of Integers to an Array.

Converting list to array in Java (with examples)

Example: Converting a list to array of string in Java

We can convert a List of Integers to an Array of Integers using the same method:

SUGGESTED READ

Example: Converting a list to array of integers in Java

Example: Creating a class to convert a list into an array

toArray(T[] arr)

This form of the same technique accepts an array that has already been defined as a parameter.

When the array’s size is more significant than or equal to the list’s size, the list’s elements are added to the array. A new array will be constructed and filled if this is not the case. On the other hand, casting is unnecessary because the parameter’s type gives the type of the returned array. If any element in the list fails to be converted into the given type, an ArrayStoreException is produced.

An array of Strings to List

You can also return from an Array to a List. We utilize Arrays.asList to accomplish this. Consider the following scenario:

get() method

The get() method is one of the most straightforward processes for converting a list to an array. We can now access all of the list elements one by one and add them to an array. The aim here is to use the get() method. We can use the list method below to get all elements and insert them into an array.

SUGGESTED READ

Return Type: The element in the list at the index specified.

The get() function of the List interface has the following syntax:

The element at the provided position in the list is returned by the get() method. Let’s look at an example of converting a list to an array to see how we may use the list’s get() method.

Example: Java program for converting a list to an array

Example: Class to convert a list into an array

Using the Stream class – introduced in Java8

Another approach is to convert a List to an array, using the Stream feature introduced in Java8. The toArray() function of the List interface has the following syntax:

The toArray() method takes either an array or none as an argument. Furthermore, it returns an array that contains all of the list’s elements. The Stream.toArray() method returns an array containing the stream entries from the List.stream() method. There are two options for doing so:

SUGGESTED READ

Using Streams in conjunction with a method reference

Using lambda expressions with streams

Let’s look at another example of converting a list to an array to see how you might use the toArray() method of the list.

Example: Program demonstrating the conversion of List to Array using stream

Using the Guava Library

There are two variations of using the Guava Library as follows.

The FluentIterable class in the Guava Library

The FluentIterable is an extended Iterable API that provides similar capabilities to Java 8’s streams library slightly differently. The goal is to create a fluent iterable that wraps an iterable list and produces an array with all its items in the iteration sequence.

The Iterables class in the Guava Library

The toArray() function of Guava’s Iterables class copies the items of an iterable into an array.

Example: using Guava

Making Use of Commons Collections

Finally, we’ll use CollectionUtils from Apache Commons Collections. To fill up the elements of an array in an empty List, use the addAll API:

SUGGESTED READ

Conclusion

In Java, converting between List and Array is a typical task. The .toArray() function is the finest and most straightforward way to transform a List into an Array in Java. Similarly, we may use the Arrays.asList() method to convert a List back to an Array. In addition, we may also use the stream() method of the Stream API to convert a list to an array in Java 8 and later.

Also, in Java, the get() and toArray() methods commonly transform a list into an array. In comparison to the get() and toArray() methods, the stream() technique is inefficient.

Source: https://www.codeunderscored.com/convert-list-to-array-in-java/