Lambda表达式 Java1.8


Lambda表达式,一看这个写法,我就联想到了ES6中的箭头函数,因为女朋友是做前端的,之前跟她一起学过一些前端的知识,不过学识尚浅,如果有什么想切磋的,直接联系我就好了。
Lambda表达式是在Java 1.8后出现的,他是针对接口中只有一个抽象方法,可用@FunctionalInterface修饰告诉JVM,这样JVM就不用挨个的去判断是否只包含一个JVM了。凡是提出某个技术,我就愿意去从它是解决什么问题而产生的说起。因为Java中收到匿名函数的支配,代码过度冗余,冗余的代码,不便于维护。例如
 
        JButton jButton = new JButton("btn");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("do something.");
            }
        });        
那么在1.8的Lambda表达式就可以
        JButton jButton = new JButton("btn");
        Button.addActionListener( e -> {
            System.out.println("hello, This is lambda.");
        });

创建线程就可以
        从
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("aaa");
            }
        });
        到
        Thread thread = new Thread(()->{
            System.out.println("HHH");
        }) ;
        thread.start();
同时,Java1.8可以允许接口中写方法的实体,它是解决当很多类都要实现一个类族,就要反复的去重写他的接口。这样代码过度冗余。

public interface MyInterface {

    void sayHello();
    
    default void sayHi() {
        System.out.println("Hi, in my interface.");
    }
    
    static void sayBye() {
        System.out.println("Bye, in my interface.");
    }
}

但是实现接口的方法需要用default或static修饰。调用时,被default修饰的方法需要在实现它的子类方法去调用。
        Impl impe = new Impl(); 
        imple.sayHi();
被static修饰的方法,需要用接口类名调用
        MyInterface.sayBye();
当出现菱形实现的时候,类A实现了接口C,类B也实现了接口C,当D实现了类A和类B,那么对于需要去重写接口中的default方法。不然就编译出错。


class AA{
    public AA(String str) {
        System.out.println(str);
    }
}

        // String为输入类型,Integer为输出类型 Integer::parseInt是进行的运算
        Function<String,Integer> function = Integer::parseInt;
        // String输入类型 返回boolean类型
        Predicate<String> predicate = str-> str.startsWith("1");
        // 执行class AA的构造方法
        Consumer<String> consumer = AA::new;
        // 输出Integer类型的123
        System.out.println(function.apply("123"));
        // 输出true
        predicate.test("123");
        // 输入 asd
        consumer.accept("asd");
        System.out.println("---------");        
        List<String> list= Arrays.asList(null,"123","asdasd","asdas","as","as","123123");
        // java 1.8中引入stream方式 去遍历输出linst中的元素
        list.stream().forEach(System.out :: println);
        System.out.println("=============");
        // dinstinct() 去重; filter(a->a !=null) 过滤掉null; 再留下所有的数字类型;再装换成double类型输出
        list.stream().distinct().filter(a->a !=null).filter(a-> Pattern.compile("^[-\\+]?[\\d]*$").matcher(a).matches()).map(Double::parseDouble).forEach(System.out::println);;
        System.out.println("-------------");    
        List<Integer> nums = Arrays.asList(1,2,3,null,2,3,3,14,3,null);
//        List<Integer> nums1 = nums.stream().filter(a -> a !=null).collect(    //ArrayList::new,ArrayList::add,ArrayList::addAll);
        // collect 就是nums数组中的非空元素放入nums1中
//        List<Integer> nums1 = nums.stream().filter(a->a!=null).collect(Collectors.toList());
//        nums1.forEach(System.out :: println);
        // reduce 以100为基数做操作。        
        System.out.println(nums.stream().filter(a->a!=null).reduce(100, (a,b)->(a+b)));
 

猜你喜欢

转载自blog.csdn.net/qq_34516081/article/details/81775781