Using extends/super in Wildcards Java/ Find the mistake

44 views Asked by At

I have this old exam, which wants me to find 2 errors in the code(there are only 2), but I have found 3.

So this is the code from the exam:

    void removeNegatives(List<? super Number> list){
     return list.stream()
     .filter(x -> x < 0)
     .foreach(System::out::println);
    }

And the exercise says the method should delete all negative numbers from a list and print out all the elements.

This is what I got:

    void removeNegatives(List<? extends Number> list){
     return list.stream()
     .filter(x -> x >= 0)
     .forEach(System.out::println);
    }

So I changed super to extends, changed the filter and the syntax of the forEach.

I think I might have gotten something wrong about the usage of extends/super with Wildcards,because our tutor told us that he never puts any mistakes in the head of the method in these exams.

Edit: I just want to know what I apparently got wrong about super/extends. I have already read the linked post and it does not help me understanding this.

0

There are 0 answers