-1
public class PositiveNumbers {
    public static List<Integer> positive(List<Integer> numbers){

        return numbers.stream()
                .mapToInt(Integer::valueOf)
                .filter(s -> s >= 0)
                .collect(Collectors.toCollection(ArrayList<Integer>::new));
    }
}


Image of the code and description of problem given by IntelliJ

Tried all that program gives as fix, asked chatGPT, but no results. I cannot see the problem.

I tried also

.collect(Collectors.toList());

but same problem...

testC0der
  • 9
  • 2
  • 1
    Habe you taken a look at the [definition of `IntStream::collect` (`docs.oracle.com`)](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/IntStream.html#collect(java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer))? The method takes three parameters. – Turing85 Jul 28 '23 at 19:44
  • `.boxed().toList();` or since you're already dealing with an Integer Stream, simply `return numbers.stream().filter(s -> s >= 0).toList();` – Hovercraft Full Of Eels Jul 28 '23 at 19:49
  • Forgot to write which one. Its Java 19 – testC0der Jul 28 '23 at 19:49

1 Answers1

3

You are using mapToInt() without any reason, you can do this way:

public static List<Integer> positive(List<Integer> numbers) {
    return numbers.stream()
                  .filter(s -> s >= 0)
                  .collect(Collectors.toList());
}

You can also, replace .collect(Collectors.toList()) with .toList(), which is available since java 16.

Diego Borba
  • 1,282
  • 8
  • 22