Guava - 按 属性 筛选并包含
Guava - Filter by Property and Contains
我正在尝试按对象中的多个属性进行过滤,在我的例子中是 CarObj:
List<String> colorList = (List<String>)filterCriteria.get("Color");
List<String> makeList = (List<String>)filterCriteria.get("Make");
List<String> rimSizeList = (List<String>)filterCriteria.get("RimSize");
Predicate<String> predColor = Predicates.in(ImmutableSet.copyOf(colorList));
Predicate<CarObj> predColor2 = Predicates.compose(predColor ,[????] );
Predicate<String> predMakeList = Predicates.in(ImmutableSet.copyOf(makeList));
Predicate<CarObj> predMakeList2 = Predicates.compose(predMakeList, [????] );
Predicate<String> predRimSize = Predicates.in(ImmutableSet.copyOf(rimSizeList));
Predicate<CarObj> predRimSize2 = Predicates.compose(predRimSize, [????] );
Collection<CarObj> filtered = FluentIterable.from(mAllCars)
.filter(predColor2)
.filter(predMakeList2)
.filter(predRimSize2)
.toList();
如何添加逻辑来处理 StartsWith 或 Contains 功能?
因此,如果用户输入 "Silver" 并且在我的 CarObjs 集合中,模型 属性 具有 "Silverado"、"Silverline"、"Silverpig"、"Tacoma", "Accord",等等...
我想找到一种方法来过滤 return 以 "Silver" 开头的汽车。它会 return 所有型号为 Silverado、Silverline 或 Silverpig 的汽车都满足其他 3 个过滤器,颜色、轮辋尺寸和品牌
在 Java 8 中,您可以只写
Predicate<CarObj> predColor2 = Predicates.compose(predColor , CarObj::getColor);
但否则你将不得不写
Predicate<CarObj> predColor2 = Predicates.compose(predColor ,
new Function<CarObj, String>() {
@Override public String apply(CarObj car) {
return car.getColor();
}
});
如果您想通过例如startsWith
,那就不要用Predicates.compose
,而是写
final Set<String> makes = ImmutableSet.copyOf(makeList);
Predicate<CarObj> predMake2 = new Predicate<CarObj>() {
@Override public boolean apply(CarObj car) {
for (String make : makes) {
if (car.getMake().startsWith(make)) {
return true;
}
}
return false;
}
};
我正在尝试按对象中的多个属性进行过滤,在我的例子中是 CarObj:
List<String> colorList = (List<String>)filterCriteria.get("Color");
List<String> makeList = (List<String>)filterCriteria.get("Make");
List<String> rimSizeList = (List<String>)filterCriteria.get("RimSize");
Predicate<String> predColor = Predicates.in(ImmutableSet.copyOf(colorList));
Predicate<CarObj> predColor2 = Predicates.compose(predColor ,[????] );
Predicate<String> predMakeList = Predicates.in(ImmutableSet.copyOf(makeList));
Predicate<CarObj> predMakeList2 = Predicates.compose(predMakeList, [????] );
Predicate<String> predRimSize = Predicates.in(ImmutableSet.copyOf(rimSizeList));
Predicate<CarObj> predRimSize2 = Predicates.compose(predRimSize, [????] );
Collection<CarObj> filtered = FluentIterable.from(mAllCars)
.filter(predColor2)
.filter(predMakeList2)
.filter(predRimSize2)
.toList();
如何添加逻辑来处理 StartsWith 或 Contains 功能? 因此,如果用户输入 "Silver" 并且在我的 CarObjs 集合中,模型 属性 具有 "Silverado"、"Silverline"、"Silverpig"、"Tacoma", "Accord",等等...
我想找到一种方法来过滤 return 以 "Silver" 开头的汽车。它会 return 所有型号为 Silverado、Silverline 或 Silverpig 的汽车都满足其他 3 个过滤器,颜色、轮辋尺寸和品牌
在 Java 8 中,您可以只写
Predicate<CarObj> predColor2 = Predicates.compose(predColor , CarObj::getColor);
但否则你将不得不写
Predicate<CarObj> predColor2 = Predicates.compose(predColor ,
new Function<CarObj, String>() {
@Override public String apply(CarObj car) {
return car.getColor();
}
});
如果您想通过例如startsWith
,那就不要用Predicates.compose
,而是写
final Set<String> makes = ImmutableSet.copyOf(makeList);
Predicate<CarObj> predMake2 = new Predicate<CarObj>() {
@Override public boolean apply(CarObj car) {
for (String make : makes) {
if (car.getMake().startsWith(make)) {
return true;
}
}
return false;
}
};