Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Lists and Arrays are two common ways of storing objects in Java. In projects where we need to store and manipulate data, both Lists and Arrays allow for storing data throughout the execution of our program.

This tutorial explains converting a List<Long> object to a Long[] Array in Java.

2. Using the List.toArray() Method

The List interface provides the toArray() method, which returns an Array object containing all the list elements.

Let’s see how to use the toArray() method to convert a List<Long> object to a Long[] Array in Java:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
Long[] array = new Long[list.size()];
array = list.toArray(array);

In the example above, we created a new Array of Long values with the same size as the List we want to convert. We can also pass an empty Array and delegate the memory allocation to the JVM:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
Long[] array = new Long[0];
array = list.toArray(array);

3. Using the Guava Library

The Guava library offers the Longs.toArray() method, which converts a collection of Long values into an Array of Long in the same order.

Let’s illustrate how to use the Guava Longs.toArray() method to convert a List<Long> object to a Long[] Array:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long[] array = Longs.toArray(list);

We should be aware that Guava’s Longs.toArray() method throws a NullPointerException if the passed Collection or any of its elements is null.

4. Using the Stream. mapToLong() Method

Java 8 allows using the Stream API to convert a List into an Array. First, we must convert the desired list to a stream using the List.stream() method. Second, we’ll apply the Stream.mapToLong() method to return a LongStream. Finally, we’ll use the Stream.toArray() method to return an Array containing the Long elements from the stream.

Let’s see now how to implement the mapToLong() method in two different ways.

First, let’s use a lambda expression:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long[] array = list.stream().mapToLong(l -> l).toArray();

Second, let’s use a method reference:

List<Long> list = Arrays.asList(1L, 2L, 3L, 4L, 5L);
long[] array = list.stream().mapToLong(Long::longValue).toArray();

5. Conclusion

In this article, we learned how to convert a List<Long> object to a Long[] array in Java using three approaches. The first approach used the List.toArray() method. In the second, we used the Guava Longs.toArray() method. And finally, we used the Java 8 Stream API via the mapToLong() method.

As always, code samples are available over on GitHub.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.