码迷,mamicode.com
首页 > 编程语言 > 详细

java8新特性之——lambda表达式的使用

时间:2018-10-22 01:11:42      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:efault   参数   put   启动   中国人   数列   class   seq   col   

lambda表达式简介

个人理解,lambda表达式就是一种新的语法,没有什么新奇的,简化了开发者的编码,其实底层还是一些常规的代码。Lambda 是一个匿名函数,我们可以把 Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。

Lambda表达式的语法(记得要在jdk1.8及以上的jdk环境下实验)

Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符
箭头操作符将 Lambda 表达式拆分成两部分:
左侧:Lambda 表达式的参数列表
右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体

###语法格式一:无参数,无返回值
示例:

//平时的写法
@Test
    public void test1() {
        Runnable runnable = new Runnable() {
            
            @Override
            public void run() {
        System.out.println("线程启动了");        
            }
        };
        runnable.run();
    }
  /**
   * 语法格式一:无参数,无返回值
   *        () -> System.out.println("Hello Lambda!");
   */
    @Test
    public void test2() {
        //“->”左边只有一个小括号,表示无参数,右边是Lambda体(就相当于实现了匿名内部类里面的方法了,(即就是一个可用的接口实现类了。))
    Runnable runnable = ()->System.out.println("线程启动了");    
    runnable.run();
    }

运行结果
技术分享图片

语法格式二:有一个参数,并且无返回值

    (x) -> System.out.println(x)

示例:

/**语法格式二:有一个参数,并且无返回值
     *      (x) -> System.out.println(x)
     */
    @Test
    public void test3() {
          //这个e就代表所实现的接口的方法的参数,
       Consumer<String> consumer = e->System.out.println("ghijhkhi"+e);
       consumer.accept("woojopj");
    }

Consumer的底层实现:

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

import java.util.Objects;

/**
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 *
 * @param <T> the type of the input to the operation
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

结果:技术分享图片

语法格式三:若只有一个参数,小括号可以省略不写 x -> System.out.println(x)</font(第二种方式的一种简化吧)

语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句

示例代码:

@Test
    public void test4() {
                //Lambda 体中有多条语句,记得要用大括号括起来
        Comparator<Integer> com = (x, y) -> {
                        System.out.println("函数式接口");
                        return Integer.compare(x, y);
                    };
                    int compare = com.compare(100, 244);
                    System.out.println(compare);
            }

Comparator的底层相信大家都看过,是有一个compare()方法的。
技术分享图片
运行结果:
技术分享图片

语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写 即:Comparator

语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”(Integer x, Integer y) -> Integer.compare(x, y);

到这儿,相信大家也看出来规律了,这个Lambda表达式,好像离不开接口咦.......你还真说对了,这个Lambda表达式,是需要函数式接口的支持的,那么什么是函数式接口呢?
函数式接口 ,即只包含一个抽象方法的接口,称为函数式接口。
你可以通过 Lambda 表达式来创建该接口的对象。(若 Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
我们可以在任意函数式接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。像上面的Consumer接口就是一个函数式接口。
Java内置的四大函数式接口分别是:
技术分享图片
consumer的上面已经演示过了。
下面是剩下的三个的

@Test
    public void test6() {
        Supplier<String> supplier = ()->"532323".substring(0, 2);
        System.out.println(supplier.get());
    }
    @Test
    public void test7() {
        Function<String, String> function = (x)->x.substring(0, 2);
        System.out.println(function.apply("我是中国人"));
    }
    @Test
    public void test8() {
        Predicate<String> predicate = (x)->x.length()>5;
        System.out.println(predicate.test("12345678"));
        System.out.println(predicate.test("123"));
    }

运行结果
技术分享图片

java8新特性之——lambda表达式的使用

标签:efault   参数   put   启动   中国人   数列   class   seq   col   

原文地址:https://www.cnblogs.com/nnxud/p/9827704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!