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 = "上海" 的userList<User> filterList = userList.stream().filter(user -> filterAgeAndAddress(user,18,"上海")).collect(Collectors.toList());filterList.forEach(System.out::println);}// 筛选 age > 18 && address = "上海" 的userprivate 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;}
}
输出如下: