Java Stream流中Filter用于通过设置的条件过滤出元素 ,示例如下:
List strings = Arrays.asList(“abc”, “”, “bc”, “efg”, “abcd”,"", “jkl”);
List filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
如果需要进行多条件过滤
我们可以对这些参数进行处理,我们只是需要做一下判断,即:
让所有参数默认为 false,然后对传进来的值进行取值判断,若满足该条件为true,多个条件进行多次判断
判断完成之后 会拿到所有的boolean值 通过 && 拼接返回,即 有一个为false,即返回值为false
通过该方法会对一个对象进行多条件过滤
public static List<BidListVo> checkOperationv1(List<BidListVo> list, BidProgressEnum progressEnum){
Integer progress = Integer.valueOf(progressEnum.getCode());
String nodeId = progressEnum.getNode();
List<BidListVo> collect = list.stream().filter(bidListVo -> filterProgress(bidListVo,progress) )
.collect(Collectors.toList());
return collect;
}
private static boolean filterProgress(BidListVo bidListVo, Integer progress) {
//副流程未开始 只判断主流程
if(StringUtils.isEmpty(bidListVo.getReviewProgress())){
return Integer.valueOf(bidListVo.getProgress()) >= progress ? true: false;
}else if(Integer.valueOf(bidListVo.getProgress()) >= progress ||
Integer.valueOf(bidListVo.getReviewProgress()) >= progress){
// 主流程 独立流程 任意一个大于当前筛选进度就好
return true;
}
return false;
}
示例
public class MultiFilterTest {
public static void main(String[] args) {
List<User> userList = getUserList();
// 筛选 age > 18 && address = "上海" 的user
List<User> filterList = userList.stream().filter(user -> filterAgeAndAddress(user,18,"上海")).collect(Collectors.toList());
filterList.forEach(System.out::println);
}
// 筛选 age > 18 && address = "上海" 的user
private static boolean filterAgeAndAddress(User user,Integer age,String address){
if(user.getAge() > 18 && address.equals(user.getAddress())){
return true;
}
return false;
}
private static List<User> getUserList() {
List<User> userList = new ArrayList<>();
userList.add(new User(1,"张三",18,"上海"));
userList.add(new User(2,"王五",16,"上海"));
userList.add(new User(3,"李四",20,"上海"));
userList.add(new User(4,"张雷",22,"北京"));
userList.add(new User(5,"张超",15,"深圳"));
userList.add(new User(6,"李雷",24,"北京"));
userList.add(new User(7,"王爷",21,"上海"));
userList.add(new User(8,"张三丰",18,"广州"));
userList.add(new User(9,"赵六",16,"广州"));
userList.add(new User(10,"赵无极",26,"深圳"));
return userList;
}
}
输出如下:文章来源:https://www.toymoban.com/news/detail-765885.html
文章来源地址https://www.toymoban.com/news/detail-765885.html
到了这里,关于Java - Stream Filter 多条件筛选过滤的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!