How can I create a list of method references?

青春壹個敷衍的年華 提交于 2019-12-05 13:02:16

It's possible your method references don't match the Consumer<String> functional interface.

This code, for example, passes compilation :

 private final static List<Consumer<String>> METHODS = Arrays.asList(
     Double::valueOf,
     Integer::valueOf,
     String::length);

Since your methods don't seem to be static, they don't match Consumer<String>, since these methods have an additional implicit parameter - the instance that the method would be applied on.

You can use a BiConsumer<TargetClass,String> :

private final static List<BiConsumer<TargetClass,String>> METHODS = Arrays.asList(
     TargetClass::setValue1,
     TargetClass::setValue2,
     TargetClass::setValue3,
     TargetClass::setValue4,
     TargetClass::setValue5);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!