402

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?

2
  • Try this link:stackoverflow.com/questions/14830313/… May 15, 2014 at 9:46
  • 2
    @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? Apr 18, 2018 at 17:35

5 Answers 5

742

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() 
8
  • 1
    @skiwi I mean, that all the other answers are unneeded after this one as they would be not so natural. May 15, 2014 at 9:56
  • 1
    Thank you I hadn't seen the boxed method yet and it worked like a charm.
    – twreid
    Jan 5, 2016 at 15:07
  • 1
    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()).
    – Lii
    Jun 29, 2016 at 9:00
  • 1
    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.
    – Lii
    Jun 29, 2016 at 9:00
  • 3
    Was tearing my hair out about what was wrong with what I had tried, thank you for pointing out the boxed() part
    – K Raphael
    Sep 28, 2017 at 15:40
25

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());
1
  • 13
    In a stroke of irony, this is what boxed() is delegating to.
    – Makoto
    Aug 30, 2018 at 20:54
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.

3
  • 6
    Although Eclipse Collections are usually quite helpful, this does not look like it is making anything easier at all :)
    – Ben
    Dec 20, 2017 at 12:32
  • 2
    Since EC 9.0, you can build a primitive list from a primitive Stream. MutableIntList list = IntLists.mutable.withAll(IntStream.range(1, 5)) Jan 11, 2019 at 21:53
  • 2
    This is what I was looking for .. boxing the int stream to Integer or to object is different thing
    – Vikash
    Apr 11, 2019 at 8:02
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.

1
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.