Java11 中的谓词过滤所有元素

Predicate in Java11 filters all elements

我要使用Java11。学习一种新方法Predicate.not,我发现我当前的代码只能找到猫科动物:

List<String> animals = List.of("cat", "leopard", "dog", "lion", "horse");
Predicate<String> cats = a -> !a.equals("dog") && !a.equals("horse");
Set<String> filterCat = animals.stream().filter(cats).collect(Collectors.toSet());
System.out.println(filterCat);

输出为:

leopard, cat, lion

现在我正在尝试使用新方法,但输出不正确。我该如何纠正它?我做错了什么?

我后面的代码:

Predicate<String> updatedCatFilter = Predicate.not(a -> a.equals("dog") && a.equals("horse"));
Set<String> catFamily = animals.stream().filter(updatedCatFilter).collect(Collectors.toSet());
System.out.println(filterCat);

但这现在输出了我所有的列表。

horse, leopard, cat, dog, lion

What did I do wrong?

您似乎缺少基本的 De-morgan's laws,其中指出

!(a || b) == !a && !b

!(a && b) == !a || !b

How do I correct it?

所以你应该更改代码以使用

Predicate.not(a -> a.equals("dog") || a.equals("horse")); // !(a || b)

这应该等同于您现有的代码

Predicate<String> cats = a -> !a.equals("dog") && !a.equals("horse");

也可以看作:

Predicate<String> notDog = a -> !a.equals("dog");
Predicate<String> notHorse =  a -> !a.equals("horse");
Predicate<String> cats = notDog.and(notHorse); // !a && !b

你可以这样写:

List<String> animals = List.of("cat", "leopard", "dog", "lion", "horse");
Set<String> filterCat = animals.stream()
    .filter(Predicate.not("dog"::equals).and(Predicate.not("horse"::equals)))
    .collect(Collectors.toSet());
System.out.println(filterCat);