英文:
How to filter in java streams using boolean condition?
问题
我正在尝试使用以下布尔条件对列表进行筛选,但这不起作用。
public void write(List<? extends Student> items) {
List<Student> list = items.stream().filter(student ->
checkAge(student.getAge())).collect(Collectors.toList());
super.write(list);
}
public boolean checkAge(String age) {
if (age.length() < 5 && age.length() > 60) {
return false;
}
return true;
}
英文:
I am trying to filter a list using boolean condition as below. But this does not work.
public void write(List<? extends Student> items) {
    List<Student> list = items.stream().filter( student ->
checkAge(student.getage())).collect(Collectors.toList());
super.write(list);
}
 public boolean checkAge(String age) {
    if (age.length() < 5 && age.length() > 60) {
        return false;
    }
    return true;
 }
答案1
得分: 1
一个数量不能同时小于5
和大于60
。因此,以下代码没有意义:
if (age.length() < 5 && age.length() > 60) {
return false;
}
您希望将其更改为类似以下的形式:
if (age.length() < 5 || age.length() > 60) {
return false;
}
英文:
A quantity can never be less than 5
and greater than 60
at the same time. So, the following code doesn't make sense:
if (age.length() < 5 && age.length() > 60) {
return false;
}
You would like to change it to something like:
if (age.length() < 5 || age.length() > 60) {
return false;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论