Querying Or Filtering Collections
优质
小牛编辑
136浏览
2023-12-01
描述 (Description)
可以使用接口Predicate在commons-collections中过滤bean的集合,并在输入对象的求值上提供true或false值。 有一个名为BeanPropertyValueEqualsPredicate的Predicate,它将根据给定值评估set属性值。
语法 (Syntax)
public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue)
上面的语法有两个参数,它们决定要评估的属性以及它的预期值。 它创建一个Predicate用于评估目标对象,如果propertyName指定的值等于propertyName指定的值,则返回true; 否则返回false。
属性名称由org.apache.commons.beanutils.PropertyUtils定义,可以是简单的,索引的,嵌套的或映射的。
例如,您可以过滤myCar属性为false的bean集合:
// create the closure
BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate( "myCar", Boolean.FALSE );
// filter the collection
CollectionUtils.filter( myCollection, predicate );
上面的代码过滤'myCollection'集合并返回对象的myCar属性的布尔值。