我有一组对象,其确切类型我不知道。让我们假设该集有4个对象A , B , C和D。
class A{
id = "test";
order = "ship";
getId();
getOrder();
},
class B {
id = "fail";
order = "ship";
getId();
getOrder();
},class C {
id = "fail";
order = "ship";
getId();
getOrder();
},class D {
id = "test";
order = "ship";
getId();
getOrder();
},
我有一个属性对象的数组,其中属性看起来像这样。
class Property {
propName : "id"; //Will never be null.
propValueMatch : "test";
},
class Property {
propName : "order"; //Will never be null.
propValueMatch : null;
}
我需要对集合进行迭代,将属性数组逻辑应用于集合中的每个对象,并返回符合条件的对象。它将像这样。
Check if the Object in Set has a readable property by the name : Property.propName. If TRUE {
if Property.propValueMatch is not null : Check if that property value of the Object has the same value as defined in Property.propValueMatch. Then the object passess the criteria and should be returned.
if Property.propValueMatch is null : Then the object passess the criteria and should be returned
}
有一个问题是,属性可能存在于我们正在迭代的对象中的一个对象变量中。
我开始实现这个功能,但我相信有一个更简单的方法,使用BeanUtils、PropertyUtils、Comaparator等来实现这个功能。
解决方案:
最后使用apache commons Predicate来创建标准来过滤集合,然后使用BeanWrapper来填充我的特定字段。