java中t和u_如何在Java的lambda表达式中使用Predicate <T>和BiPredicate <T,U>?

在java.util中定义的谓词接口。功能包。它表示一个只有一个参数的布尔值函数。它是一种函数接口,其函数方法是test()。BiPredicate接口与具有两个参数的Predicate接口类似。它可以用作lambda表达式的赋值目标。

谓词的语法@FunctionalInterface

public interface Predicate

示例import java.util.*;

import java.util.function.*;

import java.util.stream.*;

public class EmployeePredicateTest {

public static void main(String[] args) {

Employee e1 = new Employee(1, 23, "M", "Raja");

Employee e2 = new Employee(2, 13, "M", "Jai");

Employee e3 = new Employee(3, 36, "F", "Yamini");

Employee e4 = new Employee(4, 26, "F", "Geetha");

Employee e5 = new Employee(5, 19, "M", "Adithya");

List employees = new ArrayList();

employees.addAll(Arrays.asList(new Employee[]{e1, e2, e3, e4, e5}));

System.out.println(EmployeePredicate.filterEmployees(employees, EmployeePredicate.isAdultMale()));

System.out.println(EmployeePredicate.filterEmployees(employees, EmployeePredicate.isAdultFemale()));

System.out.println(EmployeePredicate.filterEmployees(employees,       EmployeePredicate.isAgeMoreThan(35)));

System.out.println(EmployeePredicate.filterEmployees(employees, EmployeePredicate.isAgeMoreThan(35).negate()));

}

}// Employee classclass Employee {

private Integer id;

private Integer age;

private String gender;

private String name;

public Employee(Integer id, Integer age, String gender, String name) {

this.id = id;

this.age = age;

this.gender = gender;

this.name = name;

}

public Integer getAge() {

return age;

}

public String getGender() {

return gender;

}

public String getName() {

return name;

}

@Override   public String toString() {

return this.name.toString()+" - "+ this.age.toString();

}

}// EmployeePredicate classclass EmployeePredicate {

public static Predicate isAdultMale() {

return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");

}

public static Predicate isAdultFemale() {

return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("F");

}

public static Predicate isAgeMoreThan(Integer age) {

return p -> p.getAge() > age;

}

public static List filterEmployees(List employees, Predicate predicate) {

return employees.stream().filter(predicate).collect(Collectors.toList());

}

}

输出结果[Raja - 23]

[Yamini - 36, Geetha - 26]

[Yamini - 36]

[Raja - 23, Jai - 13, Geetha - 26, Adithya - 19]

BiPredicate的语法@FunctionalInterface

public interface BiPredicate

示例import java.util.*;

import java.util.function.*;

public class BiPredicateTest {

public static void main(String[] args) {

BiPredicate bi = (x, y) -> x > y;

BiPredicate eq = (x, y) -> x -2 > y;

System.out.println(bi.test(2, 3));

System.out.println(bi.or(eq).test(2, 3));

System.out.println(bi.or(eq).test(8, 3));

}

}

输出结果false

false

true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值