How do I convert a Java 8 IntStream to a List?
Asked Answered
G

5

407

I'm looking at the docs for the IntStream, and I see an toArray method, but no way to go directly to a List<Integer>

Surely there is a way to convert a Stream to a List?

Geophagy answered 15/5, 2014 at 9:43 Comment(2)
Try this link:#14830813Requiem
@KarlRichter The other question doesn't give you a typed list. Also, this question was from four years ago, and has an answer with 300+ upvotes. Why are we trying to merge it now?Geophagy
L
752

IntStream::boxed

IntStream::boxed turns an IntStream into a Stream<Integer>, which you can then collect into a List:

theIntStream.boxed().collect(Collectors.toList())

The boxed method converts the int primitive values of an IntStream into a stream of Integer objects. The word "boxing" names the intInteger conversion process. See Oracle Tutorial.

Java 16 and later

Java 16 brought the shorter toList method. Produces an unmodifiable list. Discussed here.

theIntStream.boxed().toList() 
Leisha answered 15/5, 2014 at 9:48 Comment(8)
@skiwi I mean, that all the other answers are unneeded after this one as they would be not so natural.Ruthful
Thank you I hadn't seen the boxed method yet and it worked like a charm.Cybil
Addition: I think this codes gets a little shorter, clearer and prettier if you use a static import of toList. This is done by placing the following among the imports of the file: static import java.util.stream.Collectors.toList;. Then the collect call reads just .collect(toList()).Cleanly
In Eclipse it is possible to make the IDE add a static import for methods. This is done by adding the Collectors class in Preferences -> Java -> Editor -> Content Assist -> Favorites. After this, you only have to type toLi at hit Ctr+Space to have the IDE fill in toList and add the static import.Cleanly
Was tearing my hair out about what was wrong with what I had tried, thank you for pointing out the boxed() partProscription
Don't forget to import java.util.stream.Collectors;.Groceries
I was happy to get this answer -- and more broadly, these features in Java 8. Looking back after two years working in Python, and I'm so glad I don't have to deal with this nonsense.Geophagy
java stream api is un-straightforward at so many levels..Leontina
D
26

You could also use mapToObj() on a Stream, which takes an IntFunction and returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

List<Integer> intList = myIntStream.mapToObj(i->i).collect(Collectors.toList());
Daugava answered 13/10, 2016 at 8:28 Comment(1)
In a stroke of irony, this is what boxed() is delegating to.Boomer
M
9

You can use primitive collections available in Eclipse Collections and avoid boxing.

MutableIntList list = 
    IntStream.range(1, 5)
    .collect(IntArrayList::new, MutableIntList::add, MutableIntList::addAll);

Note: I am a contributor to Eclipse Collections.

Mandalay answered 27/3, 2016 at 0:6 Comment(3)
Although Eclipse Collections are usually quite helpful, this does not look like it is making anything easier at all :)Camarillo
Since EC 9.0, you can build a primitive list from a primitive Stream. MutableIntList list = IntLists.mutable.withAll(IntStream.range(1, 5))Smith
This is what I was looking for .. boxing the int stream to Integer or to object is different thingPetey
S
8

You can use the collect method:

IntStream.of(1, 2, 3).collect(ArrayList::new, List::add, List::addAll);

In fact, this is almost exactly what Java is doing when you call .collect(Collectors.toList()) on an object stream:

public static <T> Collector<T, ?, List<T>> toList() {
    return new Collectors.CollectorImpl(ArrayList::new, List::add, (var0, var1) -> {
        var0.addAll(var1);
        return var0;
    }, CH_ID);
}

Note: The third parameter is only required if you want to run parallel collection; for sequential collection providing just the first two will suffice.

Spoony answered 20/1, 2020 at 13:39 Comment(1)
That last sentence explains it all.Newcomen
P
3

Find the folowing example of finding square of each int element using Java 8 :-

IntStream ints = Arrays.stream(new int[] {1,2,3,4,5});       
List<Integer> intsList = ints.map(x-> x*x)
          .collect(ArrayList<Integer>::new, ArrayList::add, ArrayList::addAll);
Petey answered 11/4, 2019 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.