使用方法引用比较boolean
。
我想使用方法引用,而不是 (t->t)
。方法引用适用于非 boolean 值匹配,如下所述。
完整代码:
public class AccountListTest {
public static void main(String[] args){
List<Account> list = new ArrayList<>();
Account a1 = new Account(true, "Test1");
list.add(a1);
Account a2 = new Account(false, "Test2");
list.add(a2);
if(list.stream().map(Account::getName).anyMatch("test1"::equalsIgnoreCase)){
System.out.println("Contain Account with name Test1");
}
if(list.stream().map(Account::isActive).anyMatch(t->t)){
System.out.println("Have Active Account");
}
}
}
class Account{
private boolean active;
private String name;
public Account(boolean active, String name) {
super();
this.active = active;
this.name = name;
}
}
感谢任何帮助!谢谢!
最佳答案
将第二个条件写成:
if(list.stream().anyMatch(Account::isActive)) {
System.out.println("Have Active Account");
}
无需先调用map
。
关于java - 使用方法引用来检查 boolean 值匹配而不是 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492408/