zoukankan      html  css  js  c++  java
  • JDK8新特性02 Lambda表达式02_Lambda语法规则

    //函数式接口:只有一个抽象方法的接口称为函数式接口。 可以使用注解 @FunctionalInterface 修饰
    @FunctionalInterface
    public interface MyFun {
        public Integer getValue(Integer num);
    }
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.function.Consumer;
     
    import org.junit.Test;
     
    /*
     * 一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符
     *                             箭头操作符将 Lambda 表达式拆分成两部分:
     * 
     * 左侧:Lambda 表达式的参数列表
     * 右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
     * 
     * 语法格式一:无参数,无返回值
     *         () -> System.out.println("Hello Lambda!");
     * 
     * 语法格式二:有一个参数,并且无返回值
     *         (x) -> System.out.println(x)
     * 
     * 语法格式三:若只有一个参数,小括号可以省略不写
     *         x -> System.out.println(x)
     * 
     * 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
     *        Comparator<Integer> com = (x, y) -> {
     *            System.out.println("函数式接口");
     *            return Integer.compare(x, y);
     *        };
     *
     * 语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
     *         Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
     * 
     * 语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
     *         (Integer x, Integer y) -> Integer.compare(x, y);
     * 
     * 上联:左右遇一括号省
     * 下联:左侧推断类型省
     * 横批:能省则省
     * 
     * 二、Lambda 表达式需要“函数式接口”的支持
     * 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰
     *              可以检查是否是函数式接口
     */
    public class TestLambda2 {
        
        @Test
        public void test1(){
            int num = 0;//jdk 1.7 前,必须是 final
            
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    System.out.println("Hello World!" + num);
                }
            };
            
            r.run();
            
            System.out.println("-------------------------------");
            
            Runnable r1 = () -> System.out.println("Hello Lambda!");
            r1.run();
        }
        
        @Test
        public void test2(){
            Consumer<String> con = x -> System.out.println(x);
            con.accept("我大尚硅谷威武!");
        }
        
        @Test
        public void test3(){
            Comparator<Integer> com = (x, y) -> {
                System.out.println("函数式接口");
                return Integer.compare(x, y);
            };
        }
        
        @Test
        public void test4(){
            Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        }
        /**自动推断类型*/
        @Test
        public void test5(){
          String[] aaa = {"abc","def","ghi"}; //后面的值也没有写类型,根据前面自动推断出来的
    // String[] strs; // strs = {"aaa", "bbb", "ccc"}; 拆开写无法推断出类型,会报错 List<String> list = new ArrayList<>(); //java7 后面不用写类型了,因为可以根据前面推断出来,只写一个<> 即可 show(new HashMap<>());        //在Java8中,show()方法中创建的Map用泛型即可,因为可以根据方法类型自动推断出 } public void show(Map<String, Integer> map){ //方法定义中已经指定了类型 } //需求:对一个数进行运算 @Test public void test6(){ Integer num = operation(100, (x) -> x * x); System.out.println(num); System.out.println(operation(200, (y) -> y + 200)); } public Integer operation(Integer num, MyFun mf){ return mf.getValue(num); } }

    小例子:

    public class Test {
    
        /**
         * 1.调用Collection.sort()方法,通过定制排序比较2个Employee(先按年龄比,年龄一样按姓名比),使用lambda表达式传递参数
         */
    
        public static void main(String[] args) {
            List<Employee> emps = Arrays.asList(
                    new Employee(101, "张三", 18, 9999.99),
                    new Employee(102, "李四", 59, 6666.66),
                    new Employee(103, "王五", 28, 3333.33),
                    new Employee(104, "赵六", 8, 7777.77),
                    new Employee(105, "田七", 38, 5555.55)
            );
    
            Collections.sort(emps,(x1,x2) -> {
                if(x1.getAge() == x2.getAge())
                    return x1.getName().compareTo(x2.getName());
                else
                    return -Integer.compare(x1.getAge(),x2.getAge());
            });
            for (Employee emp : emps)
                System.out.println(emp);
        }
    }
    /**
         * 1.声明函数式接口,接口中声明抽象方法,public String getValue(String str);
         * 2.声明测试类,类中编写方法使用接口作为参数,
         *  1)讲一个字符串转换为大写
         *  2)去掉首位空格
         *  3)截取字符串
         */
    
    @FunctionalInterface
    public interface MyFunction {
    
        public String getValue(String str);
    }
    
    public class Test {
        public static void main(String[] args) {
       
            System.out.println(handleStr("hello world", x -> x.toUpperCase()));
            System.out.println(handleStr("			 hello world", x -> x.trim()));
            System.out.println(handleStr("hello world", x -> x.substring(0,2)));
    
        }
    
        public static String handleStr(String str, MyFunction mf) {
            return mf.getValue(str);
        }
    }
    /**
         * 1.声明一个带2个泛型的函数式接口,泛型类型为<T,R>  T为参数,R为返回值
         * 2.接口中声明对应抽象方法
         * 3.在测试类中声明方法,使用接口做为参数,计算2个Long类型参数的和
         * 4.再计算2个Long类型参数的乘积
         */
    
    
    @FunctionalInterface
    public interface MyFunction2<T,R> {
        R getValue(T t1, T t2);
    }
    
    public class Test {
    
        public static void main(String[] args) {
             System.out.println(handleLong(100L,200L,(l1,l2) -> l1 + l2));
            System.out.println(handleLong(100L,200L,(l1,l2) -> l1 * l2));
        }
    
        public static Long handleLong(Long l1,Long l2, MyFunction2<Long,Long> mf) {
            return mf.getValue(l1,l2);
        }    
  • 相关阅读:
    C语言预处理
    C语言结构体对齐
    C语言共用体、大小端、枚举
    C语言内存分配方法。
    C与指针(结构体指针,函数指针,数组指针,指针数组)定义与使用
    C语言中函数的传入值与传出值
    #define与typedef在重定义类型中的区别
    宏定义在位运算中的运用
    wait函数
    exit()与_exit()区别
  • 原文地址:https://www.cnblogs.com/guchunchao/p/10301909.html
Copyright © 2011-2022 走看看