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

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 com = (x, y) -> Integer.compare(x, y);

语法格式六: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"));
    }

 

运行结果

方法引用与构造器引用

方法引用

当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
方法引用就是Lambda表达式,就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是Lambda表达式的一个语法糖。
要求:实现抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致!
方法引用:使用操作符 “::” 将类(或对象) 与 方法名分隔开来。
如下三种主要使用情况:
对象::实例方法名
类::静态方法名
类::实例方法名

"实现抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致" 这句话很重要,一定要理解
我的理解是:
举个例子
Comparator comparator = (x,y)->Integer.compare(x, y);等同于
Comparator comparator1 = Integer::compare;
即:方法引用的方法是Integer的compare吧,他的参数列表是两个integer类型,返回值是int,这个例子中的抽象方法是Comparator接口的compare()方法

 public void test12() {
        Comparator<Integer> comparator = (x,y)->Integer.compare(x, y);
        Comparator<Integer> comparator1 = Integer::compare;
        int compare = comparator.compare(1, 2);
        int compare2 = comparator1.compare(1, 2);
        System.out.println("compare:"+compare);
        System.out.println("compare2:"+compare2);

    }

 

运行结果

compare:-1
compare2:-1

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值