Java8新特性 - Lambda表达式

Java8新特性

推荐阅读:

  • lambda表达式、方法引用、构造器引用、数组引用
  • Stream流
  • optional
  • 时间日期API [线程安全]
  • 重复注解与类型注解
  • ForkJoin框架
  • 接口的默认方法和静态方法

1. Lambda表达式

Java8中引入了一个新的操作符 “->” 该操作符称位箭头操作符

  • 定义一个接口(函数式接口)
@FunctionalInterface
public interface MyFunction {

    Integer getValue(Integer num);
}
  • 使用lambda表达式
public class LambdaTest2 {

    public Integer operation(Integer num, MyFunction mf) {
        return mf.getValue(num);
    }

    @Test
    public void test1() {
        Integer num = operation(100, (x) -> x * x);
        System.out.println(num);

        Integer num2 = operation(100, (x) -> x + 200);
        System.out.println(num2);
    }

}

1.1 内置四大核心函数式接口

接口名称接口类型方法方法参数方法返回值类型
Consumer消费型void accept(T t)×
Supplier供给型T get()×
Function<T,R>函数型R apply (T t)
Predicate断言型boolean test(T t)
消费型接口
   /**
     * Consumer<T>: 消费型接口
     * 需求:
     */
    public void happy(double money, Consumer<Double> con) {
        con.accept(money);
    }

    @Test
    public void happyTest() {
        happy(10000, m -> System.out.println("今天吃饭花了" + m));
    }
供给型接口
   /**
     * Supplier<T>:供给型接口
     * 需求:随机存储10个数字存入集合
     */
    public List<Integer> getNumList(int num, Supplier<Integer> sup) {
        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            Integer n = sup.get();
            list.add(n);
        }
        System.out.println(list);
        return list;
    }

    @Test
    public void supTest() {
        getNumList(10, () -> (int) (Math.random() * 100));
    }
函数型接口
   /**
     * Function<T,R>: 函数型接口
     */
    public String strHandler(String str, Function<String, String> fun) {
        return fun.apply(str);
    }

  
    @Test
    public void funTest() {
        String s = strHandler("\t\t\t 你是谁啊! \t\t\t", (str) -> str.trim());
        System.out.println(s);
    }
断言型接口
/**
     * Predicate<T>: 断言型接口
     * 需求:将满足条件的字符串放入集合
     */
    public static List<String> filterStr(List<String> strList, Predicate<String> pre) {
        List<String> list = new ArrayList<>();
        for (String str : strList) {
            if (pre.test(str)) {
                list.add(str);
            }
        }
        return list;
    }

    @Test
    public void preHandler(){
        List<String> stringList = Arrays.asList("Hello", "Friends", "Lambda", "www", "ok");
        List<String> strings = filterStr(stringList, (str) -> str.length() > 3);
        System.out.println(strings);
    }

1.2 方法引用

若Lambda体中的内容有方法已经实现了,我们可以使用“方法引用”(可理解为是lambda表达式的另外一种表现形式)

有三种语法形式
  • 对象::实例方法名
  • 类名::静态方法名
  • 类名::实例方法名
注意
  1. Lambda体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致!

  2. 若Lambda参数列表中的第一参数是实例方法的调用者,而第二个参数是实例方法的参数时,可使用 ClassName::methodName

对象::实例方法名
/**
     * 对象::实例方法名
     */
    @Test
    public void test1() {
        PrintStream ps = System.out;
        // 方法引用
        Consumer<String> con = ps::println;
        con.accept("aqwsdasagas");
    }
类名::静态方法名
/**
     * 类::静态方法名
     */
    @Test
    public void test2() {
        // lambda
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        // 方法引用
        Comparator<Integer> com1 = Integer::compare;
    }
类::实例方法名
/**
     * 类::实例方法名
     */
    @Test
    public void test3() {
        BiPredicate<String, String> bp = (x, y) -> x.equals(y);
        BiPredicate<String, String> bp1 = String::equals;
    }

 @Test
    public void test2() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        Comparator<Integer> com1 = Integer::compare;
    }

1.3 构造器引用

注意:需要调用的构造函数的参数列表要与函数式接口中抽象方法的参数列表保持一致!

前提定义Employee和枚举类

  • Employee
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;
    private Status status;

    public Employee(int id) {
        this.id = id;
    }
}
  • 构造器引用案例
   /**
     * 构造器引用: 获得了一个无参构造
     */
    @Test
    public void test4() {
        // lambda
        Supplier<Employee> sup = () -> new Employee();
        // 构造器引用
        Supplier<Employee> sup2 = Employee::new;
        // 应该获得了一个无参构造,因为get方法就是无参有返回值
        Employee employee = sup2.get();
        System.out.println(employee);
    }

    /**
     * 构造器引用:获取参数为int id的构造方法
     */
    @Test
    public void test5() {
        Function<Integer, Employee> fun = (x) -> new Employee(x);
        Employee apply = fun.apply(2);
        Function<Integer, Employee> fun2 = Employee::new;
        // 根据apply的参数个数去调用对应的构造方法(一个参数,int类型)
        Employee apply2 = fun.apply(3);
        System.out.println(apply);
        System.out.println(apply2);
    }

1.4 数组引用

Type::new

   /**
     * 数组引用
     */
    @Test
    public void test7(){
        Function<Integer,String[]> fun = (x) -> new String[x];
        String[] strs = fun.apply(10);
        System.out.println(strs.length);

        Function<Integer,String[]> fun2 = String[]::new;
        System.out.println(fun2.apply(15).length);
    }

1.5 情景处理

某公司员工定制排序
package cn.luis.lambda.practise;

import cn.luis.lambda.method.Employee;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Compony {

    /**
     * 某公司员工基本信息
     */
    List<Employee> emps = Arrays.asList(
            new Employee(1,"zhanagsan", 18, 1111.11),
            new Employee(2,"lisi", 49, 2222.22),
            new Employee(3,"zhaoliu", 49, 3333.33),
            new Employee(4,"tianqi", 35, 4444.44)
    );


    /**
     * 定制排序:先按年龄比,年龄相同按姓名比
     */
    @Test
    public void test1() {
        Collections.sort(emps, (e1, e2) -> {
            if (e1.getAge() == e2.getAge()) {
                return e1.getName().compareTo(e2.getName());
            } else {
                return Integer.compare(e1.getAge(),e2.getAge());
            }
        });

        System.out.println("======================");

        for (Employee emp : emps) {
            System.out.println(emp);
        }

    }

}

字符串处理
package cn.luis.lambda.practise;

import org.junit.jupiter.api.Test;

public class StringTest {
    /**
     * 函数式接口
     */
    interface StringFun {
        String getValue(String str);
    }

    /**
     * 用于处理字符串
     */
    public String strHandler(String str, StringFun sf) {
        return sf.getValue(str);
    }


    /**
     * 字符串处理
     */
    @Test
    public void test1() {
        String s1 = strHandler("\t\t\t hello , may i have a dog \t\t\t", str -> str.trim());
        System.out.println(s1);
        String s2 = strHandler("asdfghjkl", str -> str.toUpperCase());
        System.out.println(s2);
        String s3 = strHandler("asdfghjkl", str -> str.substring(0, 3));
        System.out.println(s3);
    }

}

long型数字处理
   package cn.luis.lambda.practise;

import org.junit.jupiter.api.Test;

public class LongTest {

    /**
     * 函数式接口
     */
    public interface MyFunction<T, R> {

        R getValue(T t1, T t2);

    }

    /**
     * 对于两个long型数据进行处理
     * MyFunction<Integer,Integer>: 传入参数类型、返回值类型
     */
    public void operacte(Integer x, Integer y, MyFunction<Integer, Integer> mf) {
        System.out.println(mf.getValue(x, y));
    }

    @Test
    public void test1() {
        operacte(100, 100, (x, y) -> x + y);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值