How to Convert a List<Long> to List<Integer> in Java (or vice versa!)

Let’s convert between List<Long> and List<Integer> in Java.

Suppose we’re working with the following lists:

List<Integer> ints;
List<Long> longs;

We’ll be using Long::intValue and Integer::longValue to convert between the two types.

Check out multiple ways to convert a single long value to an int.

1. Using Java 8 streams

We can convert a List<Long> to a List<Integer> using map(Long::intValue) in a stream.

ints =

And of course, we can convert a List<Integer> to a List<Long> using map(Integer::longValue).

longs =

2. Using a for loop

We can also use a traditional for loop to convert between the two types.

We’ll use the same intValue() method to convert from long to int.

for (Long num: longs) {

Then, we’ll use longValue() to convert from int to long.

for (Integer num: ints) {