一. 常用接口
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;
}