lambda表达式及函数式接口

JDK8中新添加的lambda表达式及函数式接口:
  1. lambda表达式也可以称为匿名函数,它由一个参数和方法体组成
  • lambda表达式格式
	(表达式) -> {执行语句}

  • 使用原始的匿名类不类和lambda表达式进行比较,这里是有参数有返回值
	public static void main(String[] args) {
        //使用原始的匿名类部类方式
        Comparator<String> com = new Comparator<String>() {
            public int compare(String o1, String o2) {
                return o1.compareTo(o2) ;
            }
        };
        System.out.println(com.compare("edu","eud"));

        //使用JDKlambda表达式
        Comparator<String > com2 = (o1 ,o2 ) ->  o1.compareTo(o2);
    	}

  • 演示没参数没返回值
	public static void main(String[] args) {
        //演示创建线程的任务
        Runnable run = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName()+"");
            }
        };
        //开启线程
        new Thread(run).start();
        //使用lambda表达式
        Runnable run2 =  ()->{} ;

  • lambda使用细节,参数类型推断,也就是根据接口中方法参数上的类型推断参数的类型,进而可以不写参数的类型
interface Inter{
    public void demo(int a);
}
public class Test1{
    public static void main(String[] args) {
        Inter i = a -> {
            //根据接口中的参数类型,推断这里a的类型
            System.out.println( a % 2 == 0 ? "偶数" :"奇数");
        };
    }

}

  • 如果只有一个参数是可以省略参数外的小括号,如果返回值只有一条语句可以省略返回值外的大括号
	//只有一个参数可以省略掉参数外的小括号
	Inter i = a -> {
            System.out.println( a % 2 == 0 ? "偶数" :"奇数");
        };
	//方法返回值只有一条语句,省略大括号
	Comparator<Integer> com = (o1,o2) -> o1 - o2;

  1. 函数式接口,也就是在接口中有且仅有一个抽象方法的接口,通常用注解@FunctionalInterface声明,它也可以认为是lambda接口
  • 函数式接口作为方法上的参数
@FunctionalInterface //定义此接口为函数式接口
interface Inter{
    public void demo();
}
public class Test1{
    public static void main(String[] args) {
	//调用method方法,并将函数式接口作为参数
        method( () -> {
            System.out.println(
                    "函数式接口作为参数"
            );
        });
    }
    public static void method(Inter inter){
        inter.demo();
    }
}

  • 函数式接口作为方法上的返回值
@FunctionalInterface
interface Inter{
    public void demo();
}
public class Test1{
    public static Inter method2(){
        return () -> {
            System.out.println("函数式接口作为方法返回值");
        };
    }

  1. 常用的函数式接口,在 JDK8 中的 java.util.function 包下,提供大量函数式接口
  • Supplier接口,它是一个生产者接口,其中有get方法返回任意类型的对象
@FunctionalInterface
public interface Supplier<T> {
    T get();
}
//supplier接口演示
public class Test1{
    public static void main(String[] args) {
        String s = demo(()->"123");
        System.out.println(s);
    }
    //创建方法,参数为Supplier对象
    public static <T>T demo(Supplier<T> supplier){
        return supplier.get();
    }
}

  • Consumer接口,它是一个消费者接口,其中有一个accept方法,接收任意类型的对象
@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}
//Consumer接口演示
public class Test1{
    public static void main(String[] args) {
        demo(100,(integer)->{});
    }
    public static void demo(Integer integer,Consumer<Integer> consumer){
        consumer.accept(integer);
    }
}

  • Predicate接口,它是断言接口,其中有一个test方法,接收一个任意类型的参数,返回布尔值
@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}
//Predicate接口演示
public class Test1{
    public static void main(String[] args) {
        demo(100,(x)->x % 2 ==0 ? true:false);
    }
    public static boolean demo(Integer integer,Predicate<Integer> predicate){
        return predicate.test(integer);
    }
}

  • Funcition接口,函数型接口,其中有一个apply方法,接收T类型对象,返回R类型对象
@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}
//Function接口演示
public class Test1{
    public static void main(String[] args) {
        demo(1,(x)->x+"");
    }
//接收一个Integer类型的数据,返回一个String类型的数据	
    public static String  demo(Integer i,Function<Integer ,String> function){
        String  apply = function.apply(i);
        return apply;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值