Predicate示例
在Java8中,Predicate是一个函数接口,它接收一个实参,返回一个boolean类型的结果。它通常被用来处理一个对象集合的过滤
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Predicate在filter()中使用
filter()接收predicate参数
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Predicate {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collect = list.stream().filter(x -> x > 5).collect(Collectors.toList());
System.out.println(collect); // [6, 7, 8, 9, 10]
}
}
输出
[6, 7, 8, 9, 10]
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java8Predicate {
public static void main(String[] args) {
Predicate<Integer> noGreaterThan5 = x -> x > 5;
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collect = list.stream()
.filter(noGreaterThan5)
.collect(Collectors.toList());
System.out.println(collect); // [6, 7, 8, 9, 10]
}
}
输出
[6, 7, 8, 9, 10]
Predicate在and()中使用
多个过滤
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8Predicate2 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// multiple filters
List<Integer> collect = list.stream()
.filter(x -> x > 5 && x < 8).collect(Collectors.toList());
System.out.println(collect);
}
}
输出
[6, 7]
用Predicate.and()替换
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java8Predicate2 {
public static void main(String[] args) {
Predicate<Integer> noGreaterThan5 = x -> x > 5;
Predicate<Integer> noLessThan8 = x -> x < 8;
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> collect = list.stream()
.filter(noGreaterThan5.and(noLessThan8))
.collect(Collectors.toList());
System.out.println(collect);
}
}
输出
[6, 7]
Predicate.or()
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java8Predicate3 {
public static void main(String[] args) {
Predicate<String> lengthIs3 = x -> x.length() == 3;
Predicate<String> startWithA = x -> x.startsWith("A");
List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
List<String> collect = list.stream()
.filter(lengthIs3.or(startWithA))
.collect(Collectors.toList());
System.out.println(collect);
}
}
输出
[A, AA, AAA, BBB]
Predicate.negate()
查找所有不以‘A’开头的元素
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java8Predicate4 {
public static void main(String[] args) {
Predicate<String> startWithA = x -> x.startsWith("A");
List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
List<String> collect = list.stream()
.filter(startWithA.negate())
.collect(Collectors.toList());
System.out.println(collect);
}
}
输出
[B, BB, BBB]
Predicate.test()
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java8Predicate5 {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB");
System.out.println(StringProcessor.filter(
list, x -> x.startsWith("A"))); // [A, AA, AAA]
System.out.println(StringProcessor.filter(
list, x -> x.startsWith("A") && x.length() == 3)); // [AAA]
}
}
class StringProcessor {
static List<String> filter(List<String> list, Predicate<String> predicate) {
return list.stream().filter(predicate::test).collect(Collectors.toList());
}
}
输出
[A, AA, AAA]
[AAA]
链式Predicate
import java.util.function.Predicate;
public class Java8Predicate6 {
public static void main(String[] args) {
Predicate<String> startWithA = x -> x.startsWith("a");
// start with "a" or "m"
boolean result = startWithA.or(x -> x.startsWith("m")).test("mkyong");
System.out.println(result); // true
// !(start with "a" and length is 3)
boolean result2 = startWithA.and(x -> x.length() == 3).negate().test("abc");
System.out.println(result2); // false
}
}
输出
true
false
在对象中使用Predicate
Hosting.java
public class Hosting {
private int Id;
private String name;
private String url;
public Hosting(int id, String name, String url) {
Id = id;
this.name = name;
this.url = url;
}
//... getters and setters, toString()
}
HostingRespository.java
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class HostingRespository {
public static List<Hosting> filterHosting(List<Hosting> hosting,
Predicate<Hosting> predicate) {
return hosting.stream()
.filter(predicate)
.collect(Collectors.toList());
}
}
Java8Predicate7.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Java8Predicate7 {
public static void main(String[] args) {
Hosting h1 = new Hosting(1, "amazon", "aws.amazon.com");
Hosting h2 = new Hosting(2, "linode", "linode.com");
Hosting h3 = new Hosting(3, "liquidweb", "liquidweb.com");
Hosting h4 = new Hosting(4, "google", "google.com");
List<Hosting> list = Arrays.asList(new Hosting[]{h1, h2, h3, h4});
List<Hosting> result = HostingRespository.filterHosting(list, x -> x.getName().startsWith("g"));
System.out.println("result : " + result); // google
List<Hosting> result2 = HostingRespository.filterHosting(list, isDeveloperFriendly());
System.out.println("result2 : " + result2); // linode
}
public static Predicate<Hosting> isDeveloperFriendly() {
return n -> n.getName().equals("linode");
}
}
输出
result : [Hosting{Id=4, name='google', url='google.com'}]
result2 : [Hosting{Id=2, name='linode', url='linode.com'}]