Deepak Shajan
Deepak Shajan

Reputation: 921

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.

Upvotes: 0

Views: 1835

Answers (1)

MadConan
MadConan

Reputation: 3767

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.

Upvotes: 7

Related Questions