Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW