【发布时间】:2019-02-17 09:22:28
【问题描述】:
我正在开发一个过滤器函数,它可以使用许多参数进行过滤,为此我正在使用 Java Streams。这就是我的代码:
public void filter(String cours,String prof,String salle,String group) {
this.appointments().addAll(getTimeTable().getCreneauxsList().stream()
.filter(e->e.getProf().equalsIgnoreCase(prof) )
.filter(e->e.getCours().equalsIgnoreCase(cours) )
.filter(e->e.getSalle().equalsIgnoreCase(salle) )
.filter(e->e.getGroup().equalsIgnoreCase(group) )
.collect(Collectors.toList()));
}
我想查看一个或多个参数 cours,salle,prof,group 是否为 null 或其 trim () = "",用它过滤是不值得的,因为结果不是我期望得到的。
但我不知道该怎么做。
【问题讨论】:
-
你期待什么结果?
-
在我的集合中,有一些字段为空,所以如果其中一个参数为空,我会得到这些字段,但我想要参数为空或其 trim()="" 所以没有过滤器
-
类似
e -> StringUtils.isBlank(prof) || e.getProf().equalsIgnoreCase(prof) -
所以,你想获取字段为空或空字符串的对象列表
-
否,仅当条件不为空时才应用过滤器
标签: java java-8 java-stream