简体   繁体   中英

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(). But The method to be called is identified only dynamically. I will get only the name of the function as a String value. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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