# Predicate 类基本使用详解
# 一、基本方法
test 方法主要用于参数符不符合规则。返回值 boolean
写法如下:
Predicate<String> fourLetterLong1 = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return s.length()>4 ?true:false ; | |
} | |
}; |
配合 Lambda filter 使用如下:
public static void main(String[] args) { | |
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp","Hell","opt"); | |
Predicate<String> fourLetterLong1 = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return s.length()>4 ? true : false ; | |
} | |
}; | |
names.stream() | |
.filter(fourLetterLong1) | |
.forEach((n) -> System.out.println("this is:" + n)); | |
} |
打印结果:
this is: Scala
this is: Haskell
# 二、and (Predicate<? super T> other)
default Predicate<T> and(Predicate<? super T> other) { | |
Objects.requireNonNull(other); | |
return (t) -> test(t) && other.test(t); | |
} |
and 方法等同于我们的逻辑与 && ,存在短路特性,需要所有条件都满足。第一个不满足则不走第二个
配置 Lambda filter 使用如下:
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp", "Hell", "opt"); | |
Predicate<String> startsWith = new Predicate<String>() | |
{ | |
@Override | |
public boolean test(String s) | |
{ | |
return s.equals("Haskell") ? true : false; | |
} | |
}; | |
names.stream() | |
.filter(startsWith) | |
.forEach((n) -> System.out.println("this is: " + n)); |
打印结果:
this is: Haskell
# 三、or (Predicate<? super T> other)
default Predicate<T> or(Predicate<? super T> other) { | |
Objects.requireNonNull(other); | |
return (t) -> test(t) || other.test(t); | |
} |
or 等同于我们的逻辑或 ||,多个条件只要满足一个即可。第一个满足则不走第二个
配合 Lambda filter 使用如下:
public static void main(String[] args) { | |
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp", "Hell", "opt"); | |
Predicate<String> fourLetterLong1 = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return s.length() > 4 ? true : false; | |
} | |
}; | |
Predicate<String> startsWith = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return s.equals("zzz") ? true : false; | |
} | |
}; | |
names.stream() | |
.filter(fourLetterLong1.or(startsWith)) | |
.forEach((n) -> System.out.println("this is:" + n)); | |
} |
打印结果:
this is: Scala
this is: Haskell
# 四、negate () 方法
negate 等同于我们的逻辑非!。对条件进行取反判断
配合 Lambda filter 使用如下:
public static void main(String[] args) { | |
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp", "Hell", "opt"); | |
Predicate<String> fourLetterLong1 = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return s.length() > 4 ? true : false; | |
} | |
}; | |
names.stream() | |
.filter(fourLetterLong1.negate()) | |
.forEach((n) -> System.out.println("this is:" + n)); | |
} |
打印结果:
this is:Java
this is:C++
this is:Lisp
this is:Hell
this is:opt
# 五、isEqual (Object targetRef) 方法
isEqual 类似于 equals () 区别在于:先判断对象是否为 NULL,不为 NULL 再使用 equals () 方法进行比较
static <T> Predicate<T> isEqual(Object targetRef) { | |
return (null == targetRef) | |
? Objects::isNull | |
: object -> targetRef.equals(object); | |
} |
配合 Lambda filter 使用如下:
public static void main(String[] args) { | |
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp", "Hell", "opt"); | |
Predicate<String> isEqual = new Predicate<String>() { | |
@Override | |
public boolean test(String s) { | |
return Predicate.isEqual("Java").test(s) ? true : false; | |
} | |
}; | |
names.stream() | |
.filter(isEqual) | |
.forEach((n) -> System.out.println("this is:" + n)); | |
} |
打印结果:
this is:Java
# 六、扩展
使用 Lambda 新特性和 String 类中的方法,多条件查询
public static void main(String[] args) { | |
List names = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp", "Hell", "opt"); | |
// 长度为 7 | |
Predicate<String> length = (n) -> n.length() == 4; | |
//endsWith 方法字符串是否以指定的前缀开头。 | |
Predicate<String> startsWith = (n) -> n.startsWith("J"); | |
//endsWith 字符串是否以指定的后缀结尾。 | |
Predicate<String> endsWith = (n) -> n.endsWith("a"); | |
Predicate<String> isEqual = (n) ->Predicate.isEqual("Haskell").test(n); | |
names.stream() | |
.filter(length.and(startsWith).and(endsWith).or(isEqual)) | |
.forEach((n) -> System.out.println("this is:" + n)); | |
} |
打印结果:
this is:Java
this is:Haskell