zoukankan      html  css  js  c++  java
  • JavaSE:Java8 新特性

    1.  简介

        Lambda表达式,是实例化函数式接口的重要方式,使用 Lambda 表达式可以使代码变得更加简洁紧凑

    2.  

        参数列表、箭头符号 -> 和 方法体组成, 而方法体中可以是表达式, 也可以是语句块

    3.

        语法格式:

          (参数列表) -> {方法体;}

              其中()、参数类型、{}以及 return关键字 可以省略

    4.  示例:

          Lamda表达式实现函数式接口

     1 package com.lagou.task22;
     2 
     3 import java.util.Comparator;
     4 import java.util.function.Consumer;
     5 import java.util.function.Function;
     6 import java.util.function.Predicate;
     7 import java.util.function.Supplier;
     8 
     9 public class FunctionalInterfaceTest {
    10 
    11     public static void main(String[] args) {
    12 
    13         // 1.匿名内部类的语法格式: 父类/接口类型  引用变量名 = new 父类/接口类型(){ 方法的重写 };
    14         Runnable runnable = new Runnable() {
    15             @Override
    16             public void run() {
    17                 System.out.println("我是既没有参数又没有返回值的方法!");
    18             }
    19         };
    20         runnable.run(); // 我是既没有参数又没有返回值的方法!
    21 
    22         // 使用lambda表达式实现函数式接口对象的创建: (参数列表)->{方法体;}
    23         //Runnable runnable1 = () -> { System.out.println("我是既没有参数又没有返回值的方法!"); };
    24         Runnable runnable1 = () -> System.out.println("我是既没有参数又没有返回值的方法!");
    25         runnable1.run();
    26 
    27         System.out.println("----------------------------------------------------------------------");
    28         Consumer consumer = new Consumer() {
    29             @Override
    30             public void accept(Object o) {
    31                 System.out.println(o + "有参但没有返回值的方法就是我!");
    32             }
    33         };
    34         consumer.accept("友情提示:"); // 友情提示:有参但没有返回值的方法就是我!
    35 
    36         //Consumer consumer1 = (Object o) -> {System.out.println(o + "有参但没有返回值的方法就是我!");};
    37         //Consumer consumer1 = (o) -> System.out.println(o + "有参但没有返回值的方法就是我!");
    38         // 省略了()、参数类型、{}, 自动类型推断
    39         Consumer consumer1 = o -> System.out.println(o + "有参但没有返回值的方法就是我!");
    40         consumer1.accept("友情提示:");
    41 
    42         System.out.println("----------------------------------------------------------------------");
    43         Supplier supplier = new Supplier() {
    44             @Override
    45             public Object get() {
    46                 return "无参有返回值!";
    47             }
    48         };
    49         System.out.println(supplier.get()); // 无参有返回值
    50 
    51         //Supplier supplier1 = () -> {return "无参有返回值!";};
    52         Supplier supplier1 = () -> "无参有返回值!";
    53         System.out.println(supplier1.get());
    54 
    55         System.out.println("----------------------------------------------------------------------");
    56         Function function = new Function() {
    57             @Override
    58             public Object apply(Object o) {
    59                 return o;
    60             }
    61         };
    62         System.out.println(function.apply("有参有返回值的方法")); // 有参有返回值的方法
    63 
    64         // return 和 {} 都可以省略
    65         Function function1 = o -> o;
    66         System.out.println(function1.apply("有参有返回值的方法"));
    67 
    68         System.out.println("----------------------------------------------------------------------");
    69         Comparator comparator = new Comparator() {
    70             @Override
    71             public int compare(Object o1, Object o2) {
    72                 return 0;
    73             }
    74         };
    75         System.out.println(comparator.compare(10, 20)); // 0
    76 
    77         Comparator comparator1 = (o1, o2) -> 0;
    78         System.out.println(comparator1.compare(10, 20));
    79 
    80         System.out.println("----------------------------------------------------------------------");
    81         Predicate predicate = new Predicate() {
    82             @Override
    83             public boolean test(Object o) {
    84                 return false;
    85             }
    86         };
    87         System.out.println(predicate.test("hello")); // false
    88 
    89         Predicate predicate1 = o -> false;
    90         System.out.println(predicate1.test("hello"));
    91     }
    92 }
  • 相关阅读:
    python利用pip安装模块出现报错:ReadTimeoutError: HTTPSConnectionPool的解决方法
    20届一战南理工软件工程(专硕)824
    python—socket编程
    python—异常处理
    python—面向对象设计
    python—装饰器
    python—模块与包
    python—迭代器,生成器与for循环机制
    python—文件处理
    python—函数
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/14949289.html
Copyright © 2011-2022 走看看