如何在Java 8中使用多个筛选条件并且使用OR条件?

huangapple go评论77阅读模式
英文:

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(&quot;john&quot;, &quot;a@b.c&quot;),
		new Employee(&quot;doe&quot;, &quot;a@b.c&quot;),
		new Employee(&quot;jane&quot;, &quot;x@y.z&quot;)
	);

	var filtered = allData.stream()
		.filter(emp -&gt;  &quot;jane&quot;.equals(emp.empName()) || &quot;x@y.z&quot;.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 -&gt; emp.hasNameOrEmail(&quot;jane&quot;, &quot;x@y.z&quot;))
	    .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&lt;Employee&gt; hasName(String lookingFor) {
	return emp -&gt; lookingFor.equals(emp.empName());
}
public Predicate&lt;Employee&gt; hasEmail(String lookingFor) {
	return emp -&gt; lookingFor.equals(emp.empEmailId());
}

@Test
void test() {
	// ...		
	var filtered = allData.stream()
		.filter(hasName(&quot;jane&quot;).or(hasEmail(&quot;x@y.z&quot;)))
		.collect(Collectors.toList());
}

huangapple
  • 本文由 发表于 2023年3月3日 21:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定