Predicate介绍
Predicate是Java 8中的一个函数式接口,它代表了一个函数,该函数接受一个参数并返回一个boolean值。Predicate接口通常用于过滤集合或流中的元素。它可以用于定义各种复杂的条件,例如判断元素是否满足某种条件或符合某种规则。
Predicate接口定义了一个抽象方法test(T t)
,该方法用于确定给定参数是否满足该Predicate的条件。如果满足条件,则返回true,否则返回false。
Predicate接口还提供了一些默认方法来实现常见的操作,例如与、或和非操作。这些方法可以用于组合多个Predicate,以创建更复杂的条件。
下面是一个使用Predicate的示例:
Predicate<Integer> isEven = num -> num % 2 == 0;
System.out.println(isEven.test(4)); // 输出 true
System.out.println(isEven.test(5)); // 输出 false
在这个示例中,我们定义了一个Predicate isEven,它用于判断一个整数是否是偶数。我们用isEven的test方法测试了两个整数,输出结果分别为true和false。
使用Predicate可以简化代码,并提高可读性。它可以与Java 8中的流API非常好地结合使用,用于过滤、映射和聚合数据。
Predicate使用
FilteringApples 示例展示了如何使用Predicate过滤苹果。
public class FilteringApples {
public static void main(String ... args){
List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
new Apple(155, "green"),
new Apple(120, "red"));
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
System.out.println(greenApples);
// [Apple{color='green', weight=155}]
List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
System.out.println(heavyApples);
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
System.out.println(greenApples2);
// [Apple{color='green', weight=155}]
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
System.out.println(heavyApples2);
// []
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
"brown".equals(a.getColor()));
System.out.println(weirdApples);
}
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
public static List<Apple> filterHeavyApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if (apple.getWeight() > 150) {
result.add(apple);
}
}
return result;
}
public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
}
public static boolean isHeavyApple(Apple apple) {
return apple.getWeight() > 150;
}
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
public static class Apple {
private int weight = 0;
private String color = "";
public Apple(int weight, String color){
this.weight = weight;
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}';
}
}
}
策略模式使用Predicate
使用策略模式消灭if else,可以利用Java8的新特性来实现策略模式。利用Java8的Predicate消灭 if else。
首先定义一个map,key是不同的服务代码,value是需要做校验的条件,然后针对不同的服务代码做校验。当然Supplier、Consumer都可以做类似的实现。
// 定义校验的策略映射关系
static Map<String, Predicate<Model>> predicateMap = new HashMap<>();
static {
predicateMap.put("A", s -> {
return StringUtils.isNotBlank(s.getName())
&& (StringUtils.isNotBlank(s.getAge()) || StringUtils.isNotBlank(s.getPhone()));
});
predicateMap.put("B", s -> {
return StringUtils.isNotBlank(s.getName())
&& StringUtils.isNotBlank(s.getPhone());
});
}
public static void main(String[] args) {
List<String> codes = Lists.newArrayList("A","B");
for (String service : codes) {
if (!predicateMap.containsKey(service)) {
failResponse.setResultMsg("代码" + service + "未上线");
return failResponse;
}
boolean test = predicateMap.get(service).test(s);
if (!test) {
failResponse.setResultMsg("代码" + service + "入参校验失败");
return failResponse;
}
}
}
}