Java8四大核心函数式接口及方法引用和构造器引用

Consumer<T> : 消费型接口 void accept(T t);

//Consumer<T> 消费型接口 :
@Test
public void test1(){
    happy(10000, (m) -> System.out.println("你们刚哥喜欢大宝剑,每次消费:" + m + "元"));
} 
public void happy(double money, Consumer<Double> con){
    con.accept(money);
}

Supplier<T> : 供给型接口 T get();

//Supplier<T> 供给型接口 :
@Test
public void test2(){
    List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
    for (Integer num : numList) {
        System.out.println(num);
    }
}
​
//需求:产生指定个数的整数,并放入集合中
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);
    }
    return list;
}

Function<T, R> : 函数型接口 R apply(T t);

//Function<T, R> 函数型接口:
@Test
public void test3(){
    String newStr = strHandler("\t\t\t 我大尚硅谷威武   ", (str) -> str.trim());
    System.out.println(newStr);
​
    String subStr = strHandler("我大尚硅谷威武", (str) -> str.substring(2, 5));
    System.out.println(subStr);
}
​
//需求:用于处理字符串
public String strHandler(String str, Function<String, String> fun){
    return fun.apply(str);
}

Predicate<T> : 断言型接口 boolean test(T t);

//Predicate<T> 断言型接口:
@Test
public void test4(){
    List<String> list = Arrays.asList("Hello", "atguigu", "Lambda", "www", "ok");
    List<String> strList = filterStr(list, (s) -> s.length() > 3);
​
    for (String str : strList) {
        System.out.println(str);
    }
}
​
//需求:将满足条件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> pre){
    List<String> strList = new ArrayList<>();
    for (String str : list) {
        if(pre.test(str)){
            strList.add(str);
        }
    }
    return strList;
}

其他接口

方法引用和构造器引用

package com.atguigu.java8;
​
import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
​
import org.junit.Test;
​
/*
 * 一、方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用
 *             (可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
 * 
 * 1. 对象的引用 :: 实例方法名
 * 
 * 2. 类名 :: 静态方法名
 * 
 * 3. 类名 :: 实例方法名
 * 
 * 注意:
 *      ①方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
 *      ②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
 * 
 * 二、构造器引用 :构造器的参数列表,需要与函数式接口中参数列表保持一致!
 * 
 * 1. 类名 :: new
 * 
 * 三、数组引用
 * 
 *     类型[] :: new;
 * 
 * 
 */
public class TestMethodRef {
   //数组引用
   @Test
   public void test8(){
      Function<Integer, String[]> fun = (args) -> new String[args];
      String[] strs = fun.apply(10);
      System.out.println(strs.length);
      
      System.out.println("--------------------------");
      
      Function<Integer, Employee[]> fun2 = Employee[] :: new;
      Employee[] emps = fun2.apply(20);
      System.out.println(emps.length);
   }
   
   //构造器引用
   @Test
   public void test7(){
      Function<String, Employee> fun = Employee::new;
      
      BiFunction<String, Integer, Employee> fun2 = Employee::new;
   }
   
   @Test
   public void test6(){
      Supplier<Employee> sup = () -> new Employee();
      System.out.println(sup.get());
      
      System.out.println("------------------------------------");
      
      Supplier<Employee> sup2 = Employee::new;
      System.out.println(sup2.get());
   }
   
   //类名 :: 实例方法名
   @Test
   public void test5(){
      BiPredicate<String, String> bp = (x, y) -> x.equals(y);
      System.out.println(bp.test("abcde", "abcde"));
      
      System.out.println("-----------------------------------------");
      
      BiPredicate<String, String> bp2 = String::equals;
      System.out.println(bp2.test("abc", "abc"));
      
      System.out.println("-----------------------------------------");
      
      
      Function<Employee, String> fun = (e) -> e.show();
      System.out.println(fun.apply(new Employee()));
      
      System.out.println("-----------------------------------------");
      
      Function<Employee, String> fun2 = Employee::show;
      System.out.println(fun2.apply(new Employee()));
      
   }
   
   //类名 :: 静态方法名
   @Test
   public void test4(){
      Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
      
      System.out.println("-------------------------------------");
      
      Comparator<Integer> com2 = Integer::compare;
   }
   
   @Test
   public void test3(){
      BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
      System.out.println(fun.apply(1.5, 22.2));
      
      System.out.println("--------------------------------------------------");
      
      BiFunction<Double, Double, Double> fun2 = Math::max;
      System.out.println(fun2.apply(1.2, 1.5));
   }
​
   //对象的引用 :: 实例方法名
   @Test
   public void test2(){
      Employee emp = new Employee(101, "张三", 18, 9999.99);
      
      Supplier<String> sup = () -> emp.getName();
      System.out.println(sup.get());
      
      System.out.println("----------------------------------");
      
      Supplier<String> sup2 = emp::getName;
      System.out.println(sup2.get());
   }
   
   @Test
   public void test1(){
      PrintStream ps = System.out;
      Consumer<String> con = (str) -> ps.println(str);
      con.accept("Hello World!");
      
      System.out.println("--------------------------------");
      
      Consumer<String> con2 = ps::println;
      con2.accept("Hello Java8!");
      
      Consumer<String> con3 = System.out::println;
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自由自在1039

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值