9

I have an array of [5, 6, 7, 3, 9], I would like to change each element from the array substracting by 2, then store the in a Set, so what I did is

Set<Integer> mySet = Arrays.stream(arr1).map(ele -> new Integer(ele - 2)).collect(Collectors.toSet());

but I am getting two exceptions here as

  1. The method collect(Supplier<R>, ObjIntConsumer<R>, BiConsumer<R,R>) in the type IntStream is not applicable for the arguments (Collector<Object,?,Set<Object>>)"
  2. Type mismatch: cannot convert from Collector<Object,capture#1-of ?,Set<Object>> to Supplier<R>

What does those error mean and how can I fix the issue here with Java Stream operation?

5
  • 1
    Dont use new Integer(...), it is deprecated for good reason. Use Integer.valueOf(...) instead. Commented Apr 27, 2020 at 6:16
  • 1
    Thank you @Zabuzard, that was a nice hint! Commented Apr 27, 2020 at 6:21
  • 1
    Closely related if not duplicate - How do I convert a Java 8 IntStream to a List? Commented Apr 27, 2020 at 6:25
  • @Naman Very closely related, but I am not sure if it is a duplicate, because OP doesn't seem to have known that they were working with an IntStream Commented Apr 27, 2020 at 6:30
  • @Lino at least the error message reads that explicitly, just some debugging efforts away if I could say so. Commented Apr 27, 2020 at 6:39

3 Answers 3

12

It looks like arr1 is an int[] and therefore, Arrays.stream(arr1) returns an IntStream. You can't apply .collect(Collectors.toSet()) on an IntStream.

You can box it to a Stream<Integer>:

Set<Integer> mySet = Arrays.stream(arr1)
                           .boxed()
                           .map(ele -> ele - 2)
                           .collect(Collectors.toSet());

or even simpler:

Set<Integer> mySet = Arrays.stream(arr1)
                           .mapToObj(ele -> ele - 2)
                           .collect(Collectors.toSet());
Sign up to request clarification or add additional context in comments.

2 Comments

yes, you're right, the arr1 is an int[], so does it mean .boxed().map(ele -> ele - 2) and .mapToObj(ele -> ele - 2) both returns an IntStream? and is boxed() only could be applied on IntStream to convert into an Stream<Integer>?
No, they both return a Stream<Integer>. They are applied on IntStream. And yes, boxed() converts an IntStream to a Stream<Integer>.
7

Arrays.stream(int[]) returns an IntStream. And IntStream does not offer collect() methods that take a Collector.

If you need to use Collectors.toSet(), then you need a Stream<Integer> for it, and you can call mapToObj for that:

Set<Integer> mySet = Arrays.stream(arr1)
                           .mapToObj(ele -> ele - 2)
                           .collect(Collectors.toSet());

Comments

0

If you're open to using a third-party library, you can avoid boxing the int values as Integer using Eclipse Collections IntSet.

int[] array = {5, 6, 7, 3, 9};
IntStream stream = Arrays.stream(array).map(value -> value - 2);
IntSet actual = IntSets.mutable.ofAll(stream);
IntSet expected = IntSets.mutable.of(3, 4, 5, 1, 7);
Assert.assertEquals(expected, actual);

Note: I am a committer for Eclipse Collections.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.