java入门篇(34) Lambda表达式

一、Lambda表达式

1.1 Lambda表达式的概念

Lambda表达式就是对匿名内部类的再次简写。不是所有的匿名内部类都可以用Lambda表达式简写,只有函数式借口(接口中只有一个抽象方法)才能用Lambda表达式表达。

举例:不带返回值

public class Text {
    public static void main(String[] args) {
       Runnable myrunnable = new Runnable() {
           @Override
           public void run() {
               System.out.println("匿名内部类");
           }
       };
       Thread thread = new Thread(myrunnable);
       thread.start();
       Runnable myrunable1 = ()-> System.out.println("匿名内部类1");
       Thread b = new Thread(myrunable1);
       b.start();
    }
}

待返回值:

public class Text {
    public static void main(String[] args) {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return  o1 - o2;
            }
        };
        Comparator<Integer> comparator1 = (x,y) ->{return x-y;};
        Integer[] integer = new Integer[]{1,5,3,2};
        Arrays.sort(integer, comparator1);
       for(Integer in : integer) {
           System.out.print(in + " ");
       }
    }
}

1.2 Lambda表达式的语法规则

  • ->左边是参数列表,右边是方法的具体实现。
  • 左边参数列表里的参数不用写类型,他会自己推断出参数的类型
  • 如果右边具体实现里只有一行代码,则不用写{}个return。
  • 可以用@FunctionalInterface来检测借口是不是函数式接口。

接口:

@FunctionalInterface
public interface myInterface {
    int show();
}

public class Text {
    public static void main(String[] args) {
        new myInterface() {
            @Override
            public int show() {
                System.out.println("show方法");
                return 0;
            }
        }.show();
        myInterface ny = ()-> {
            System.out.println("show1");
            return 0;
        };
        ny.show();
    }
}

结果:
show方法
show1

二、java提供的四大核心函数式接口

函数式接口参数类型返回类型用途
Consumer消费型接口Tvoid对类型为T的对象应用操作,包含方法:void accept(T t)
Supplier供给型接口TT返回类型为T的对象,包含方法: T get();
Function<T, R>函数型接口TR对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法: R apply(T t);
Predicate 断言型接口Tboolean确定类型为T的对象是否满足某约束,并返回boolean 值。包含方法boolean test(T t);

Consumer:

public class Text {
    public static void main(String[] args) {
        Consumer<String> consumer = (s) -> System.out.println(s);
        consumer.accept("^(* ̄(oo) ̄)^");
    }
}

结果:
^(* ̄(oo) ̄)^

Supplier:

public class Text {
    public static void main(String[] args) {
        Supplier<Student> suplier = new Supplier<Student>() {
            @Override
            public Student get() {
                return new Student();
            }
        };
        Student stu = suplier.get();
        stu.name = "水桥舞";
        stu.age = "23";
        System.out.println(stu.name + " " + stu.age);
    }
}


Function:

public class Text {
    public static void main(String[] args) {
        Function<Student,String> function = (student)-> {
            return student.name + " " + student.age;
        };
        Student student = new Student();
        student.age = "12";
        student.name = "^(* ̄(oo) ̄)^";
        System.out.println(function.apply(student));
    }
}

结果:
^(* ̄(oo) ̄)^ 12

Predicate:

public class Text {
    public static void main(String[] args) {
        Predicate<String> predicate = (String str)->{
            if(str.equals("^(* ̄(oo) ̄)^"))
            return true;
            else return false;
        };
        String str = "^(* ̄(oo) ̄)^";
        String str1 = "老虎";
        System.out.println(predicate.test(str));
        System.out.println(predicate.test(str1));
    }
}

结果:
true
false

三、方法引用

3.1 方法引用的概念

b

3.2 方法引用的格式

调用方法的类名::调用的方法名

3.3 举例

1.调用随机数函数

public class Text {
    public static void main(String[] args) {
        Supplier<Double> supplier = Math::random;
        System.out.println(supplier.get().toString());
    }
}

2.调用比较函数

public class Text {
    public static void main(String[] args) {

    //正常的式子
    Comparator<Integer> comparator = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
           return Math.max(o1,o2);
        }
    };
    //Lambda表达式
    Comparator<Integer> comparator1 = (o1, o2)->Math.max(o1,o2);
    //方法引用
    Comparator<Integer> comparator2 = Math::max;
        System.out.println(comparator.compare(3, 5));
        System.out.println(comparator1.compare(3, 5));
        System.out.println(comparator2.compare(3, 5));
    }
}

结果:
5
5
5

3.输出语句

public class Text {
    public static void main(String[] args) {

    //正常的式子
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
    //Lambda表达式
    Consumer<String> consumer1 = (str)-> System.out.println(str);
    //方法引用
    Consumer<String> consumer2 = System.out::println;
    consumer.accept("^(* ̄(oo) ̄)^");
    consumer1.accept("^(* ̄(oo) ̄)^");
    consumer2.accept("^(* ̄(oo) ̄)^");
    }
}

结果:
^(* ̄(oo) ̄)^
^(* ̄(oo) ̄)^
^(* ̄(oo) ̄)^

3.4 小结

  • Lambda表达式返回的是new一个T的实例对象时,可以用方法引用:T::new来表示
 public class Text {
    public static void main(String[] args) {

    //正常的式子
       Supplier<Student> supplier = new Supplier<Student>() {
           @Override
           public Student get() {
               Math.random();
               return new Student();
           }
       };
    //Lambda表达式
    Supplier<Student> supplier1 = ()->new Student();
    //方法引用
    Supplier<Student> supplier2 = Student::new;
        Student student = supplier2.get();
        student.name = "^(* ̄(oo) ̄)^";
        student.age = "18";
        System.out.println(student.name + " " + student.age);
    }
}

结果:
^(* ̄(oo) ̄)^ 18
  • 如果Lambda表达式返回null,并且调用了一个方法,则可以用方法引用表示,T::调用的方法名。
public class Text {
    public static void main(String[] args) {
        Student student1 = new Student();
        student1.name = "zhang";
        student1.age = "19";
    //正常的式子
      Function<Student,String> function = new Function<Student, String>() {
          @Override
          public String apply(Student student) {
              String s = String.valueOf(100);
              return s;
          }
      };
    //Lambda表达式
   Function<Student,String> function1 = (student)->{
       String s = String.valueOf(100);
       return  s;
   };
    //方法引用
    Function<Student,String> function2 = String::valueOf;
        System.out.println(function2.apply(student1));
        System.out.println(function1.apply(student1));
        System.out.println(function.apply(student1));
    }
}

结果:
Demo.Student@682a0b20
100
100
  • 方法引用可以简写原来的代码,但是不一定能表达出原代码的含义,可能会出错,他和Lambda表达式只是类型意义上的简单匹配。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值