看代码:
if(new Predicate(){
@Override
public boolean test(Person person) {
return “男”.equals(person.sex);
}
}.test(jack)){
System.out.println(jack.name + “是男的!”);
}
如果这种Predicate对象其他方法也需要用怎么办?有一个技巧就是单独做一个方法,返回这种独有的Predicate对象。
/** 好耶!做成static任何地方想用就用啦! * */
public static Predicate PredicateBody (){
return new Predicate(){
@Override
public boolean test(Person person) {
return “男”.equals(person.sex);
}
};
}
然后是用的地方:
Person jack = new Person(“jack”,“男”);
if(PredicateBody().test(jack)){
System.out.println(jack.name + “是男的!”);
}
最后附上Predicate源码:
/*
-
Copyright © 2010, 2013, Oracle and/or its affiliates. All rights reserved.
-
ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util.function;
import java.util.Objects;
/**
-
Represents a predicate (boolean-valued function) of one argument.
-
This is a functional interface
-
whose functional method is {@link #test(Object)}.
-
@param the type of the input to the predicate
-
@since 1.8
*/
@FunctionalInterface
public interface Predicate {
/**
-
Evaluates this predicate on the given argument.
-
@param t the input argument
-
@return {@code true} if the input argument matches the predicate,
-
otherwise {@code false}
*/
boolean test(T t);
/**
-
Returns a composed predicate that represents a short-circuiting logical
-
AND of this predicate and another. When evaluating the composed
-
predicate, if this predicate is {@code false}, then the {@code other}
-
predicate is not evaluated.
-
Any exceptions thrown during evaluation of either predicate are relayed
-
to the caller; if evaluation of this predicate throws an exception, the
-
{@code other} predicate will not be evaluated.
-
@param other a predicate that will be logically-ANDed with this
-
predicate
-
@return a composed predicate that represents the short-circuiting logical
-
AND of this predicate and the {@code other} predicate
-
@throws NullPointerException if other is null
*/
default Predicate and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
/**
-
Returns a predicate that represents the logical negation of this
-
predicate.
-
@return a predicate that represents the logical negation of this
-
predicate
*/
default Predicate negate() {
return (t) -> !test(t);
}
/**
-
Returns a composed predicate that represents a short-circuiting logical
-
OR of this predicate and another. When evaluating the composed
-
predicate, if this predicate is {@code true}, then the {@code other}
-
predicate is not evaluated.
-
Any exceptions thrown during evaluation of either predicate are relayed
-
to the caller; if evaluation of this predicate throws an exception, the
-
{@code other} predicate will not be evaluated.
-
@param other a predicate that will be logically-ORed with this
-
predicate
必看视频!获取2024年最新Java开发全套学习资料 备注Java
@return a composed predicate that represents the short-circuiting logical
-
OR of this predicate and the {@code other} predicate
-
@throws NullPointerException if other is null
*/
default Predicate or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
/**
-
Returns a predicate that tests if two arguments are equal according
-
to {@link Objects#equals(Object, Object)}.
-
@param the type of arguments to the predicate
-
@param targetRef the object reference with which to compare for equality,
-
which may be {@code null}
-
@return a predicate that tests if two arguments are equal according
-
to {@link Objects#equals(Object, Object)}
*/
static Predicate isEqual(Object targetRef) {
return (null == targetRef)
-
? Objects::isNull
- object -> targetRef.equals(object);
}
}
Predicate自带的and方法怎么用?
=====================
看下源码:
default Predicate and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
意思就是加一个other的断言规则,返回的是自己(当前规则)和other规则判定的最终值(已经是一个布尔值了)之间的并集。
举个例子,一个person对象,我需要去判断他的name是否是jack,并且sex是否是男,代码如下。
Person jack = new Person(“jack”,“男”);
if(new Predicate(){
@Override
public boolean test(Person person) {
return “jack”.equals(person.name);
}
}.and(p -> “男”.equals(p.sex)).test(jack)){
System.out.println(jack.name + “是男的!”);
}else{
System.out.println(“判断出错!”);
}
以上代码可以得到预期的结果。
但是,相信很多人对and方法的源码已经看糊涂了,我这边再来点一点,其实刚才发生的事情就是这样:
Predicate p1 = new Predicate() {
@Override
public boolean test(Person person) {
return “jack”.equals(person.name);
}
};
Predicate p2 = t -> true && true;
&& 左右两边的条件必须都为true,结果才是true。
至于为什么能这样写,站长也觉得挺奇怪的,姑且就认为是Java8对lamda内部做的特别处理吧,有点像lamda的扩展语法。也就是说,lamda支持 “参数平移”,下面的这个例子,&&右边的逻辑相当于左边lamda的扩展,是一段用于参数 t 的 新逻辑。不得不说, 这真的很巧妙。
Person rose = new Person(“rose”,“女”);
Predicate isGirlAndRose = t -> “女”.equals(t.sex) && “rose”.equals(t.name);
if(isGirlAndRose.test(rose)){
System.out.println(“是rose!”);
}
函数式接口总结
=======
1.如果一个接口里面有且仅有一个未实现的方法,它就是函数式接口,你可以直接用lamda来完成new的操作。
2.如果一个方法的返回值是函数式接口,return的时候直接写个lamda即可。
3.如果参数是函数式接口,你可以直接写一个lamda,相当于传了一个匿名子类对象进去。
Lamda方法引用 ::(双冒号)
=================
Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使 Java的语言表达能力得到了提升。
方法引用 ::
=======
方法引用是什么 ?
方法引用是用来直接访问类或者实例的已经存在的方法或者构造方法。方法引用提供了一
种引用而不执行方法的方式,它需要由兼容的函数式接口构成的目标类型上下文。计算时,方法引用会创建函数式接口的一个实例。
当Lambda表达式中只是执行一个方法调用时,不用Lambda表达式,直接通过方法引用的形式可读性更高一些。
- 作用
方法引用的唯一用途是支持Lambda的简写。
方法引用提高了代码的可读性,也使逻辑更加清晰。
- 组成
使用::操作符将方法名和对象或类的名字分隔开。
“::” 是域操作符(也可以称作定界符、分隔符)。
静态方法引用
ClassName::staticMethod
实例方法引用
objectName::instanceMethod
构造方法引用(构造器引用)
最后
按照上面的过程,4个月的时间刚刚好。当然Java的体系是很庞大的,还有很多更高级的技能需要掌握,但不要着急,这些完全可以放到以后工作中边用别学。
学习编程就是一个由混沌到有序的过程,所以你在学习过程中,如果一时碰到理解不了的知识点,大可不必沮丧,更不要气馁,这都是正常的不能再正常的事情了,不过是“人同此心,心同此理”的暂时而已。
“道路是曲折的,前途是光明的!”
g-blog.csdnimg.cn/img_convert/e76d4cb40751fe4b33a27e6d6dbc472c.png)
静态方法引用
ClassName::staticMethod
实例方法引用
objectName::instanceMethod
构造方法引用(构造器引用)
最后
按照上面的过程,4个月的时间刚刚好。当然Java的体系是很庞大的,还有很多更高级的技能需要掌握,但不要着急,这些完全可以放到以后工作中边用别学。
学习编程就是一个由混沌到有序的过程,所以你在学习过程中,如果一时碰到理解不了的知识点,大可不必沮丧,更不要气馁,这都是正常的不能再正常的事情了,不过是“人同此心,心同此理”的暂时而已。
“道路是曲折的,前途是光明的!”
[外链图片转存中…(img-VczQRwtl-1716355722261)]
[外链图片转存中…(img-eGg1tohr-1716355722261)]