写在前面-函数推导
1.静态方法的推导
2.实例的某方法 参考第四部分
3.构造方法 参考第五部分及其他
1 consumer接口test
定义一个consumer接口的使用方法
private static <T> void useConsumer(Consumer<T> consumer, T t) {
consumer.accept(t);
}
方法推导过程
Consumer<String> consumer = (s) -> System.out.println(s);
useConsumer(consumer, "Hello test1");
useConsumer(s -> System.out.println(s), "Hello Alex");
//最后这部分,由于consumer接口中的accept方法是void类型,与print方法返回及传参类型一致即可使用以下函数推导进行改写
useConsumer(System.out::println, "Hello Wangwenjun");
2 list接口的输出
List<Apple> list = Arrays.asList(new Apple("abc", 123), new Apple("Green", 110), new Apple("red", 123));
System.out.println(list);
list.sort((a1, a2) -> a1.getColor().compareTo(a2.getColor()));
System.out.println(list);
list.stream().forEach(a -> System.out.println(a));
System.out.println("==========================");
list.stream().forEach(System.out::println);
输出内容如下
[Apple{color='abc', weight=123}, Apple{color='Green', weight=110}, Apple{color='red', weight=123}]
[Apple{color='Green', weight=110}, Apple{color='abc', weight=123}, Apple{color='red', weight=123}]
Apple{color='Green', weight=110}
Apple{color='abc', weight=123}
Apple{color='red', weight=123}
其中forEach方法在arrayList中 源码如下,即循环每一个list中的对象,并对其使用Consumer的accept方法,即传入一个Consumer接口的实现类即可使用该方法,进而使用这种函数推导的方式,且返回一个void类型,
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
3 Function接口 Apply的方法推导
首先看Function接口的源码定义
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
即传入某个类型,返回另一个类型,接下我们使用这个接口,完成传入String,返回Int这样的一个功能;
//1.定义一个使用方法
private static <T,R> R useApply(T input,Function<T,R> f){
return f.apply(input);
}
//2.使用函数推导进行代码编写
Integer integer1 = useApply("123", s -> Integer.parseInt(s));
//3.表达式再化简
Integer integer = useApply("321", Integer::parseInt);
上面的测试部分也可以这样写,上面几个基本上是在匿名内部类环节进行函数推导,当然也可以使用函数推导的方式直接对接口进行实现
//使用函数推导的方式,直接构建一个接口实现类,并使用即可,
Function<String, Integer> f = Integer::parseInt;
Integer result = f.apply("123");
System.out.println(result);
4 使用实例对象的方法推导test
String string = new String("Hello");
Function<Integer, Character> function = string::charAt;
System.out.println(function.apply(2));
输出结果为 l,即第三个字母
5 构造函数的推导
//无参构造函数,使用supplier接口进行接收
Supplier<String> supplier = String::new;
String s = supplier.get();
System.out.println(s);
//两个参数的构造方法使用BiFunction接口,apple构造是传入String和Long,使用BiFunction
BiFunction<String, Long, Apple> appleFuntion = Apple::new;
Apple apple = appleFuntion.apply("red", 100L);
System.out.println(apple);
6多参数构造函数的推导
可以自己创建一个类似于BiFunction的接口,即可,内部的接口方法,传参为多个,返回值为一个即可,如下
6.1 构造函数实体类,传入三个参数构造
public class ComplexApple
{
private String color;
private long weight;
private String name;
public ComplexApple(String color, long weight, String name) {
this.color = color;
this.weight = weight;
this.name = name;
}
public String getColor() {
return color;
}
public long getWeight() {
return weight;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "ComplexApple{" +
"name='" + name + '\'' +
", weight=" + weight +
", color='" + color + '\'' +
'}';
}
}
6.2 构建一个function Interface
@FunctionalInterface
public interface ThreeFunction<T, U, K, R> {
R apply(T t, U u, K k);
}
6.3 推导代码
ThreeFunction<String, Long, String, ComplexApple> threeFunction = ComplexApple::new;
ComplexApple complexApple = threeFunction.apply("Green", 123L, "FuShi");
System.out.println(complexApple);
得到以下结果
ComplexApple{name=‘FuShi’, weight=123, color=‘Green’}