在Java 8中,Predicate<T>是一个函数式接口,用于对某个类型的对象进行条件判断,返回一个布尔值。
Predicate<T>接口定义如下:
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Predicate<T>接口只有一个抽象方法 test,该方法接受一个泛型参数并返回一个布尔值。你可以使用Lambda表达式或方法引用来实现 Predicate<T> 接口,根据自己的需求编写条件判断代码。
以下是 Predicate<T> 简单的示例用法:
Predicate<Integer> isEven = num -> num % 2 == 0;
System.out.println(isEven.test(4)); // 输出: true
System.out.println(isEven.test(7)); // 输出: false
上述示例创建了一个用于判断一个整数是否为偶数的 Predicate<Integer> 对象 isEven。通过调用 test 方法,你可以传入一个整数,然后得到对应的布尔值结果。
以下是 Predicate<T> 常用的示例用法:
1. 过滤列表 可以使用Predicate来过滤列表中的元素。通过实现test方法,可以定义一个条件,然后使用stream的filter方法来过滤满足该条件的元素
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class PredicateExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isGreaterThan5 = num -> num > 5;
List<Integer> filteredNumbers = numbers.stream()
.filter(isEven.and(isGreaterThan5))
.collect(Collectors.toList());
System.out.println(filteredNumbers); // 输出: [6, 8, 10]
}
}
2. 判断是否为空 可以使用Predicate来判断一个对象是否为空,可以通过实现test方法来定义判断条件。
Predicate<String> isNullOrEmpty = str -> str == null || str.isEmpty();
System.out.println(isNullOrEmpty.test(null)); // 输出 true
System.out.println(isNullOrEmpty.test("")); // 输出 true
System.out.println(isNullOrEmpty.test("abc")); // 输出 false
3. 判断字符串是否匹配某个模式 可以使用Predicate来判断一个字符串是否匹配某个模式,可以通过实现test方法来定义判断条件。
Predicate<String> isMatch = str -> str.matches("\\d+");
System.out.println(isMatch.test("123")); // 输出 true
System.out.println(isMatch.test("abc")); // 输出 false
4. 校验输入参数 可以使用Predicate来校验输入参数的合法性,可以通过实现test方法来定义校验条件。
Predicate<Integer> isValidAge = age -> age >= 18 && age <= 65;
System.out.println(isValidAge.test(20)); // 输出 true
System.out.println(isValidAge.test(16)); // 输出 false
5. Predicate<T> 接口还提供了一些默认方法,如 and、or、negate,用于进行条件的组合、取反等操作。
以下是三个默认方法的说明和示例:
5.1. and(Predicate<? super T> other):返回一个组合判断条件,要同时满足当前条件和传入的另一个条件。
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isGreaterThan5 = num -> num > 5;
Predicate<Integer> isEvenAndGreaterThan5 = isEven.and(isGreaterThan5);
System.out.println(isEvenAndGreaterThan5.test(6)); // 输出: true
System.out.println(isEvenAndGreaterThan5.test(3)); // 输出: false
}
}
上述示例中,我们创建了两个 Predicate<Integer> 对象 isEven 和 isGreaterThan5,分别用于判断数字是否为偶数和是否大于5。然后我们使用 and 方法将这两个条件进行逻辑与操作,生成了一个新的 Predicate<Integer> 对象 isEvenAndGreaterThan5。通过调用 test 方法,我们可以检查一个数字是否同时满足偶数和大于5的条件。
5.2. or(Predicate<? super T> other):返回一个组合判断条件,满足当前条件或者传入的另一个条件即可。
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isGreaterThan5 = num -> num > 5;
Predicate<Integer> isEvenOrGreaterThan5 = isEven.or(isGreaterThan5);
System.out.println(isEvenOrGreaterThan5.test(4)); // 输出: true
System.out.println(isEvenOrGreaterThan5.test(7)); // 输出: true
System.out.println(isEvenOrGreaterThan5.test(3)); // 输出: false
}
}
在上述示例中,我们使用 or 方法将 isEven 和 isGreaterThan5 这两个条件进行逻辑或操作,生成了一个新的 Predicate<Integer> 对象 isEvenOrGreaterThan5。通过调用 test 方法,我们可以检查一个数字是否满足偶数或者大于5的条件。
5.3. negate():返回当前判断条件的取反条件。
import java.util.function.Predicate;
public class PredicateExample {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;
Predicate<Integer> isNotEven = isEven.negate();
System.out.println(isNotEven.test(3)); // 输出: true
System.out.println(isNotEven.test(4)); // 输出: false
}
}
在上述示例中,我们使用 negate 方法对 isEven 条件进行取反操作,生成了一个新的 Predicate<Integer> 对象 isNotEven。通过调用 test 方法,我们可以检查一个数字是否不满足偶数的条件。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。