java8函数推导

写在前面-函数推导

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’}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

灵湖映北辰

年轻人,要讲武德!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值