简体   繁体   English

Java 8 Streams中的动态函数调用

[英]Dynamic function calling in Java 8 Streams

List<?> temp = empObjList.stream()
                    .filter(nestedDo -> nestedDo.getAttrib1() == "subject")
                    .collect(Collectors.toList());

Here I'm calling the method getAttrib1(). 在这里,我将调用方法getAttrib1()。 But The method to be called is identified only dynamically. 但是要调用的方法只能动态识别。 I will get only the name of the function as a String value. 我将只获得函数名称作为String值。 I want to convert it dynamically to function. 我想将其动态转换为功能。 I know I can use Reflections for the dynamic method calling, but I can't rewrite the above code by reflection. 我知道我可以将Reflections用于动态方法调用,但是我不能通过反射来重写上面的代码。

I think what you're looking for is something more like 我认为您正在寻找的更像是

List<?> temp = empObjList.stream().filter(this::processDo)
                    .collect(Collectors.toList());

Where processDo(nestedDo) is a method that does the reflection to figure out what method to call on the do. 其中processDo(nestedDo)是一种进行反射以找出在do上调用哪种方法的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM