0

I have the following code:

class inner {
    Integer i;
    public Integer getValue() {
        return i;
    }
    public void setValue(Integer i) {
        this.i = i;
    }
}

class outer {
    public static inner i1;

    outer(Integer i) {
        i1.setValue(i);
    }
}

public class MyClass{
public void main() {
    List<Integer> ll = Arrays.asList(new outer(2)).stream().map(outer.i1::getValue).collect(Collectors.toList());

}

I get the following error:

required: Function<? super Object,? extends R>
  found: outer.i1::getValue
  reason: cannot infer type-variable(s) R
    (argument mismatch; invalid method reference
      method getValue in class inner cannot be applied to given types
        required: no arguments
        found: Object
        reason: actual and formal argument lists differ in length)
  where R,T are type-variables:
    R extends Object declared in method <R>map(Function<? super T,? extends R>)

I am new to streams, and reading the docs doesn't clear this up for me. Any help will be appreciated.

2
  • 2
    Your code is very strange. Why are you using a constructor to modify a static value? Why are you trying to map an instance of outer to a static instance of inner? What do you expect it to produce? Commented Jan 8, 2017 at 9:47
  • @shmosel This is just a test code I wrote to understand stream API's working. Commented Jan 8, 2017 at 11:24

1 Answer 1

4

getValue is a method that takes no arguments. When you try to pass a method reference of getValue to Stream's map method, you are trying to pass the Stream's element to getValue, but getValue doesn't take any arguments.

You can replace the method reference with a lambda expression if you wish to ignore the outer element of the Stream:

List<Integer> ll = Arrays.asList(new outer(2)).stream().map(o -> outer.i1.getValue()).collect(Collectors.toList());

However, this will lead to a NullPointerException, since you don't initialize public static inner i1 anywhere, so invoking the outer constructor will throw that exception.

It's hard to suggest how to fix that without knowing what you are trying to do.

I don't know if it makes sense to have a static member of type inner in your outer class, but if it makes sense, you should probably initialize it in a static initializer block (or in its declaration).

You could, for example, change

public static inner i1;

to

public static inner i1 = new inner();

which will eliminate the exception.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I got it. The method reference passed to map is applied to the object if it is applicable to the object types of the stream. But then there is also the case where you can pass method references which take the stream objects as arguments. I have explained it here: stackoverflow.com/questions/41532611/…

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.