27

I wrote this code:

import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String... args) {
        List<String> a = Arrays.asList("One", "Two", "three");
        List<Integer> lengths = a.stream().mapToInt(String::length).collect(Collectors.toList());

    }
}

but it doesn't want to compile, saying:

Error:(8, 68) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
  required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
  found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.List<java.lang.Object>>
  reason: cannot infer type-variable(s) R
    (actual and formal argument lists differ in length)

What's happening here? Why the limitation? If you use map. Instead of mapToInt, it works fine.

2
  • In case it helps Google find this, the error message I get from trying this now is "The method collect(Supplier<R>, ObjIntConsumer<R>, BiConsumer<R,R>) in the type IntStream is not applicable for the arguments (Collector<Object,?,List<Object>>)".
    – Noumenon
    Commented Feb 29, 2020 at 18:31
  • Try this : a.stream().map(String::length).collect(Collectors.toList()); Commented Jun 13, 2020 at 19:04

3 Answers 3

30

mapToInt produces an IntStream, which doesn't have collect method taking a single Collector argument.

Since the end result is a List<Integer>, you don't have to transform the Stream to an IntStream:

List<Integer> lengths = a.stream().map(String::length).collect(Collectors.toList());

Transforming the Stream to an IntStream would make sense if you want to collect the elements of the Stream to an primitive array:

int[] lengths = a.stream().mapToInt(String::length).toArray();

If you want to transform to an IntStream and still use the collect method, you can write the following (which is less recommended):

List<Integer> lengths = 
    a.stream()
     .mapToInt(String::length)
     .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
5
  • 7
    simple way a.stream().mapToInt(String::length).boxed().collect(Collectors.toList());
    – Hadi
    Commented May 27, 2018 at 12:50
  • 2
    @HadiJ how is it simpler than just changing mapToInt to map and not having to call boxed()?
    – Eran
    Commented May 27, 2018 at 12:51
  • I mean instead of this a.stream() .mapToInt(String::length) .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    – Hadi
    Commented May 27, 2018 at 12:52
  • 2
    @HadiJ I was just demonstrating how to use collect on an IntStream. If you call boxed(), you are no longer using an IntStream, so you might as well not map to an IntStream in the first place, as mentioned at the beginning of my answer.
    – Eran
    Commented May 27, 2018 at 12:54
  • 1
    There might be a point in using boxed() if you need a List<Long> or List<Double>: List<Long> lengths = a.stream().mapToLong(String::length).boxed().collect(Collectors.toList());
    – Holger
    Commented May 28, 2018 at 7:35
7

There is a simple way to transform IntStream to Stream. Add boxed() as an intermediate operation and you will be able to use collect() as a terminal operation.

List<Integer> lengths = a.stream().mapToInt(String::length).boxed().collect(Collectors.toList());
2

After .mapToInt you got an IntStream.

The collect definition for this stream is:

<R> R collect(Supplier<R> supplier,
              ObjIntConsumer<R> accumulator,
              BiConsumer<R, R> combiner);

Hence you cannot simply do toList() here.

Your Answer

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

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