Lambda常用函数接口与方法引用

一. 常用接口

1. Consumer<T>

消费型函数,传入T 无返回void

2. Function<T, R>

函数型,传入T 返回R

3. Predicate<T>

预言型函数,传入T 返回boolean

4. Supplier<T>

供给型, 无入参,返回T

先定义个POJO,后续示例使用

class Apple {
    private int weight = 0;
    private String color = "";
    
    public Apple(){}
    
    public Apple(int weight) {
        this.weight = weight;
    }
    public Apple(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }
	// 省略Getter Setter toString
}
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @description: jdk 提供的常用函数接口示例
 * @author: adaivskenan
 * @time: 2022/6/12 10:18 PM
 */
public class LambdaJdk {


    /**
     * Consumer<T>
     *      void accept(T t);
     *
     * Function<T, R>
     *      R apply(T t);
     *
     * Predicate<T>
     *     boolean test(T t);
     *
     * Supplier<T>
     *     T get();
     * */

    /**
     * Consumer: 消费型函数,传入T 无返回void
     * */
    @Test
    public void testConsumer(){
        consumer("Lambda", s -> System.out.println("你好," + s));
    }

    public void consumer(String str, Consumer<String> consumer){
        consumer.accept(str);
    }

    /**
     * Function: 函数型,传入T 返回R
     * */
    @Test
    public void testFunction(){
        System.out.println(function("hello, lambda", s -> s.toUpperCase()));
    }

    public String function(String str, Function<String, String> function){
        return function.apply(str);
    }

    /**
     * Predicate: 预言型函数,传入T 返回boolean
     * */
    @Test
    public void testPredicate(){
        List<String> words = Arrays.asList("Hello", "World", "Lambda", "Function");
        // 过滤并打印包含字母'o'的字符串
        predicate(words, s -> s.contains("o")).stream().forEach(System.out::println);
    }

    public List<String> predicate(List<String> words, Predicate<String> predicate){
        return words.stream().filter(predicate).collect(Collectors.toList());
    }


    /**
     * Supplier: 供给型, 无入参,返回T
     * */
    @Test
    public void testSupplier(){
        System.out.println("supplier(() -> new Apple()) = " + supplier(() -> new Apple()));
        supplierList(10,() -> new Apple(1,"red"));
    }

    public Apple supplier(Supplier<Apple> supplier){
        return supplier.get();
    }

    public List<Apple> supplierList(int num, Supplier<Apple> supplier){
        List<Apple> appleList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            appleList.add(supplier.get());
        }
        return appleList;
    }

}

二. 方法引用

1. 类名::new

构造器的参数列表与函数式接口中抽象方法参数需匹配

 /**
   * 构造方法引用,每次构建不同的对象
   * */
  @Test
  public void testMethodRefrence(){
      Apple red1 = getApple("red", 1);
      Apple red2 = getApple("red", 1);
      Assert.assertNotEquals("每次都是单独构建", red1, red2);
  }

  /**
   * Map存储构造对象函数
   * */
  static Map<String, Function<Integer,Apple>> appleMap = new HashMap<>();
  {
      appleMap.put("red", Apple::new);
      appleMap.put("green", Apple::new);
      appleMap.put("blue", Apple::new);
  }

  public Apple getApple(String color, Integer weight){
      return appleMap.get(color).apply(weight);
  }

2. 对象::示例方法名

/**
   * 对象::实例方法名
   * */
  @Test
  public void testObjectMethodRefrence(){
  	// out 为输出流对象
      Consumer<String> consumer = System.out::println;
  }

3. 类名::静态方法名

 /**
   * 类名::静态方法名
   * */
  @Test
  public void testStaticMethodRefrence(){
	// 相当于(x,y)-> Integer.compare(x,y);
  	Comparator<Integer> comparator = Integer::compare;
  }

4. 类名::实例方法名

 /**
   * 类名::实例方法名
   * */
  @Test
  public void testClassObjectMethodRefrence(){
	// (x,y) -> x.equals(y); 符合该模式:参数x类型 为对象类型, y为参数类型时
  	BiPredicate<String,String> bip = String::equals;
  	
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值