通过代码对java的lambda表达式和方法引用进行了解

静态方法引用

静态方法Util

public class Util {
    public static int StringLength(String s){
        return s.length();
    }
}

函数式接口TestFunctional

@FunctionalInterface //用来约束这个接口只有一个抽象方法
public interface TestFunctional {
    int toLength(String s);
}

测试类进行调用接口的实现类对象的toLength方法

public class Test {
    public static void main(String[] args) {
        //调用接口的实现类对象的toLength静态方法
        //创建实现类实现TestFunctional接口,并重写toLength,然后使用实现类调用toLength静态方法==>匿名内部类
        
        //匿名内部类
        TestFunctional a=new TestFunctional() {
            @Override
            public int toLength(String s) {
                return Util.StringLength(s);
            }
        };
        //原始lambda表达式
        TestFunctional a=(String s)->{
                return Util.StringLength(s);
        };
        //简化后
        TestFunctional a=(s)-> Util.StringLength(s);
        //方法引用
        TestFunctional a=Util::StringLength;
    }
}

实例方法引用

实例方法Util

public class Util {
    public  int StringLength(String s){
        return s.length();
    }
}

函数式接口TestFunctional

@FunctionalInterface
public interface InstanceFunctional {
    int toLength(String s);
}

测试类进行调用接口的实现类对象的toLength方法

public class Test {
    public static void main(String[] args) {
        Util util = new Util();
        //调用接口的实现类对象的toLength方法
        //创建实现类实现TestFunctional接口,并重写toLength,然后再new创建实现类对象==>匿名内部类
        //匿名内部类
        InstanceFunctional functional=new InstanceFunctional() {
            @Override
            public int toLength(String s) {
                return util.StringLength(s);
            }
        };
        //原始lambda表达式
        InstanceFunctional functional=(String s)->{
                return util.StringLength(s);
        };
        //简化后
        InstanceFunctional functional=s-> util.StringLength(s);
        //方法引用
        InstanceFunctional functional=util::StringLength;
    }
}

构造函数引用

public class Person {
    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }
}

函数式接口TestFunctional

@FunctionalInterface
public interface ConstructorFunctional {
    Person create(String name);
}

测试类进行调用接口的实现类对象的create方法

public class Test {
    public static void main(String[] args) {
        //调用接口的实现类对象的create方法
        //创建实现类实现TestFunctional接口,并重写create,然后再new创建实现类对象==>匿名内部类
        //匿名内部类
        ConstructorFunctional functional=new ConstructorFunctional() {
            @Override
            public Person create(String name) {
                return new Person(name);
            }
        };
        //原始lambda表达式
        ConstructorFunctional functional=(String name)->{
                return new Person(name);
        };
        //简化后
        ConstructorFunctional functional=name->new Person(name);
        //方法引用
        ConstructorFunctional functional=Person::new;
    }
}

特定类型的任意对象的实例方法引用

函数式接口TestFunctional

@FunctionalInterface
public interface SpecialFunctional {
    int toLength(String s);
}

测试类进行调用接口的实现类对象的toLength方法

public class Test {
    public static void main(String[] args) {
        //调用接口的实现类对象的toLength方法
        //创建实现类实现TestFunctional接口,并重写toLength,然后再new创建实现类对象==>匿名内部类
        //匿名内部类
        SpecialFunctional special=new SpecialFunctional() {
            @Override
            public int toLength(String s) {
                return s.length();
            }
        };
        //原始lambda表达式
        SpecialFunctional special=(String s)->{
                return s.length();
        };
        //简化后
        SpecialFunctional special=s->s.length();
        //方法引用
        SpecialFunctional special=String::length;
    }
}

特定类型的任意对象的实例方法引用举例

方法引用类型函数式接口定义实例方法方法引用表达式使用场景描述
特定类型的任意对象的实例方法引用interface StringCheck { boolean check(String s); }boolean isEmpty()String::isEmpty检查字符串是否为空
特定类型的任意对象的实例方法引用interface ListSize { int size(List<?> list); }int size()List::size获取列表的大小
特定类型的任意对象的实例方法引用interface ThreadStatus { boolean isAlive(Thread t); }boolean isAlive()Thread::isAlive检查线程是否仍然活跃
特定类型的任意对象的实例方法引用interface NumberSign { int getSign(BigDecimal number); }int signum()BigDecimal::signum获取BigDecimal数值的符号(正数、负数或零)
特定类型的任意对象的实例方法引用interface StringOperation { String operate(String s); }String toLowerCase()String::toLowerCase转换字符串为小写
特定类型的任意对象的实例方法引用interface IntValue { int getInt(Integer integer); }int intValue()Integer::intValue获取Integer的原始int值
特定类型的任意对象的实例方法引用interface BoolValue { boolean getBool(Boolean bool); }boolean booleanValue()Boolean::booleanValue获取Boolean的原始boolean值
特定类型的任意对象的实例方法引用interface EmptyCheck { boolean isEmpty(Collection<?> collection); }boolean isEmpty()Collection::isEmpty检查集合是否为空
特定类型的任意对象的实例方法引用interface DoubleValue { double getDouble(Double d); }double doubleValue()Double::doubleValue获取Double的原始double值

示例 1: 使用 String 类的 isEmpty 方法

首先,定义一个函数式接口:

@FunctionalInterface
interface StringCheck {
    boolean check(String s);
}

匿名内部类

StringCheck check1 = new StringCheck() {
    @Override
    public boolean check(String s) {
        return s.isEmpty();
    }
};

Lambda表达式

StringCheck check2 = s -> s.isEmpty();

方法引用

StringCheck check3 = String::isEmpty;

示例 2: 使用 List 类的 size 方法

定义一个接口来处理列表:

@FunctionalInterface
interface ListSize {
    int size(List<?> list);
}

匿名内部类

ListSize size1 = new ListSize() {
    @Override
    public int size(List<?> list) {
        return list.size();
    }
};

Lambda表达式

ListSize size2 = list -> list.size();

方法引用

ListSize size3 = List::size;

示例 3: 使用 Thread 类的 isAlive 方法

定义一个接口来检查线程状态:

@FunctionalInterface
interface ThreadStatus {
    boolean isAlive(Thread t);
}

匿名内部类

ThreadStatus status1 = new ThreadStatus() {
    @Override
    public boolean isAlive(Thread t) {
        return t.isAlive();
    }
};

Lambda表达式

ThreadStatus status2 = t -> t.isAlive();

方法引用

ThreadStatus status3 = Thread::isAlive;

示例 4: 使用 BigDecimal 类的 signum 方法

定义一个接口来获取数字的符号:

@FunctionalInterface
interface NumberSign {
    int getSign(BigDecimal number);
}

匿名内部类

NumberSign sign1 = new NumberSign() {
    @Override
    public int getSign(BigDecimal number) {
        return number.signum();
    }
};

Lambda表达式

NumberSign sign2 = number -> number.signum();

方法引用

NumberSign sign3 = BigDecimal::signum;

示例 5: 使用 String 类的 toLowerCase 方法

定义一个接口来转换字符串为小写:

@FunctionalInterface
interface StringOperation {
    String operate(String s);
}

方法引用

StringOperation lower = String::toLowerCase;

示例 6: 使用 Integer 类的 intValue 方法

定义一个接口来获取 Integer 对象的原始 int 值:

@FunctionalInterface
interface IntValue {
    int getInt(Integer integer);
}

方法引用

IntValue intValue = Integer::intValue;

示例 7: 使用 Boolean 类的 booleanValue 方法

定义一个接口来获取 Boolean 对象的原始 boolean 值:

@FunctionalInterface
interface BoolValue {
    boolean getBool(Boolean bool);
}

方法引用

BoolValue boolValue = Boolean::booleanValue;

示例 8: 使用 Collection 类的 isEmpty 方法

定义一个接口来检查任何类型的集合是否为空:

@FunctionalInterface
interface EmptyCheck {
    boolean isEmpty(Collection<?> collection);
}

方法引用

EmptyCheck empty = Collection::isEmpty;

示例 9: 使用 Double 类的 doubleValue 方法

定义一个接口来获取 Double 对象的原始 double 值:

@FunctionalInterface
interface DoubleValue {
    double getDouble(Double d);
}

方法引用

DoubleValue doubleValue = Double::doubleValue;
  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值