1
这里是我的父类动态函数调用Java中8流与谓词对象
class Parent
{
String attrib1;
String attrib2;
String attrib3;
// getters and setters of three fields
然后创建一个列表
List<Parent> objList = new ArrayList<Parent>();
然后,我添加了一些父对象来objList。
现在我想根据类中字段的值来过滤这些对象。但是我只会动态地获取字段名称。 我想为此使用流。
List<Parent> temp = objList.stream()
.filter(nestedDo -> nestedDo.getAttrib2() == "manu")
.collect(Collectors.toList());
这里getAttrib2()变化。它可以是getAttrib1()或getAttrib3()。
所以我需要动态函数调用。我们可以使用谓词来实现它吗?不幸的是,我对Predicate对象一无所知。请用它里面的所有概念来详细解释你的答案。
Reflection or Predicate在这里会更好吗? –
这种情况下需要反射和谓词的组合。你需要编写逻辑来选择确切的方法'名称',并通过反射在谓词表达式 – Abhishek
@Abhishek:你能解释一下如何详细使用反射。我尝试了很多解决使用反射,但我无法解决它。 –