Syntax for specifying a method reference to a generic method

不羁的心 提交于 2019-12-01 16:27:25

The second quoted point just means that the type parameter belongs to the class. For example:

class MyClass<T>
{
    public boolean myGenMeth(T x, T y)
    {
        boolean result = false;
        // ...
        return result;
    }
}

This would then be called like this:

SomeTest<Integer> mRef = new MyClass<Integer>() :: myGenMeth;

For example

  Predicate<List<String>> p = List<String>::isEmpty;

Actually we don't need the type argument here; the type inference will take care of

  Predicate<List<String>> p = List::isEmpty;

But in cases type inference fails, e.g. when passing this method reference to a generic method without enough constraints for inference, it might be necessary to specify the type arguments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!