Java - Stream Filter multi-condition filtering

Filter in Java Stream is used to filter out elements according to the set conditions. The example is as follows:

List strings = Arrays.asList(“abc”, “”, “bc”, “efg”, “abcd”,"", “jkl”);

List filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

If you need to perform multi-condition filtering

We can process these parameters, we just need to make a judgment, that is:

Let all parameters default to false, and then judge the value of the passed in value. If the condition is met, it is true, and multiple conditions are judged multiple times.

After the judgment is completed, all boolean values ​​will be obtained and returned through && splicing, that is, one of them is false, that is, the return value is false.

This method will perform multi-condition filtering on an object.

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;
    }

Example 

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;
    }
}

The output is as follows:

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/131033831