英文:
how to use multiple filter using OR conditon in java 8
问题
如何在Java 8中使用多个过滤器并使用OR条件:
return getAllData().stream()
.filter(emp -> emp.getEmpName().equals(empName) || emp.getEmpEmailId().equals(empEmailId))
.collect(Collectors.toList());
我尝试过这样做,但它不接受我的.getEmailId()
。
有人能帮忙吗?
英文:
how to use multiple filter using OR conditon in java 8
return getAllData().stream()
.filter(emp -> emp.getEmpName().equals(empName) && emp->emp.getEmpEmailId().equals(empEmailId))
.collect(Collectors.toList());
i've tried this butnits not accepting my .getEmailId()
can anyone please help
答案1
得分: 1
简单的解决方案是使用简单的'or'表达式:
```java
@Test
void test() {
var allData = List.of(
new Employee("john", "a@b.c"),
new Employee("doe", "a@b.c"),
new Employee("jane", "x@y.z")
);
var filtered = allData.stream()
.filter(emp -> "jane".equals(emp.empName()) || "x@y.z".equals(emp.empName()))
.collect(Collectors.toList());
}
如果过滤条件描述了关于这个“employee”特定的内容,您可以将其封装在类内部,像这样:
record Employee(String empName, String empEmailId) {
private boolean hasNameOrEmail(String name, String email) {
return name.equals(this.empName) || email.equals(this.empEmailId);
}
}
@Test
void test() {
// ...
var filtered = allData.stream()
.filter(emp -> emp.hasNameOrEmail("jane", "x@y.z"))
.collect(Collectors.toList());
}
最后,如果您想创建更通用的东西,您可以声明不同的'Predicate'对象,可以使用.or()
链接:
public Predicate<Employee> hasName(String lookingFor) {
return emp -> lookingFor.equals(emp.empName());
}
public Predicate<Employee> hasEmail(String lookingFor) {
return emp -> lookingFor.equals(emp.empEmailId());
}
@Test
void test() {
// ...
var filtered = allData.stream()
.filter(hasName("jane").or(hasEmail("x@y.z")))
.collect(Collectors.toList());
}
英文:
A simple solution will be to use the simple 'or' expression:
@Test
void test() {
var allData = List.of(
new Employee("john", "a@b.c"),
new Employee("doe", "a@b.c"),
new Employee("jane", "x@y.z")
);
var filtered = allData.stream()
.filter(emp -> "jane".equals(emp.empName()) || "x@y.z".equals(emp.empName()))
.collect(Collectors.toList());
}
If the filtering criteria is describing something specific about this employee
, you can encapsulate it inside the class, like this:
record Employee (String empName, String empEmailId) {
private boolean hasNameOrEmail(String name, String email) {
return name.equals(this.empName) || email.equals(this.empEmailId);
}
}
@Test
void test() {
// ...
var filtered = allData.stream()
.filter(emp -> emp.hasNameOrEmail("jane", "x@y.z"))
.collect(Collectors.toList());
}
Lastly, if you want to create something more generic, you can declare different Predicate
objects that can be chained with .or()
:
public Predicate<Employee> hasName(String lookingFor) {
return emp -> lookingFor.equals(emp.empName());
}
public Predicate<Employee> hasEmail(String lookingFor) {
return emp -> lookingFor.equals(emp.empEmailId());
}
@Test
void test() {
// ...
var filtered = allData.stream()
.filter(hasName("jane").or(hasEmail("x@y.z")))
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论