常用函数式接口--1.8特性

在这里插入图片描述
函数式接口大概的结构就如上图,下面直接上代码,关于一些原理并没有介绍。
1.Supplier接口 生产对象 一个抽象

/*
1)生产对象接口Supplier<T>
抽象方法:T get(); 生产对象由lambda完成
 */
public class Test01 {
    public static void main(String[] args) {
        String s="abc";
        show(()->{return "helloWorld";});
    }
    private static void show(Supplier<String> lambda ) {
        String s1 = lambda.get();
        System.out.println(s1);
    }
}

2.Consumer接口 消费一个对象 一个抽象一个默认

/*消费对象接口Consumer<T>
抽象方法void accept(T t);有参无返回
*/
public class Test02 {
    public static void main(String[] args) {
        String str="123";
        method(str,s-> System.out.println(s));
    }
    public static void method(String s, Consumer<String> lambda) {
        lambda.accept(s);
    }
}
/*
消费对象接口Consumer<T>
默认方法void andThen(T t);有参无返回
 */
public class Test03 {
    public static void main(String[] args) {
        String s="hello";
        //输出字符串的长度,并且把字符串中的所有字母转换为大写
        method(s,s1-> System.out.println(s1.length()),
                 s1-> System.out.println(s1.toUpperCase()));
    }
    private static void method(String s, Consumer<String> one,Consumer<String> two) {
        one.andThen(two).accept(s);
    }
}

3.Predicate接口 判断 一个抽象三个默认(与或非)

/*消费对象接口Predict<T>
抽象方法boolean test(T t);//有参有返回
*/
//判断字符串中是否含有a字符
public class Test04 {
    public static void main(String[] args) {
        String str="hello";
        boolean boo = method(str, s -> str.contains("a"));
        System.out.println(boo);
    }

    private static boolean method(String s,Predicate<String> lambda) {
        return lambda.test(s);
    }
}
/*判断对象接口Predict<T>
默认方法default Predicate<T> and(Predicate<? super T> other)//有参有返回  全真为真
*/
//需求:如果要判断一个字符串既要包含字母“a”,并且长度大于5。
public class Test05 {
    public static void main(String[] args) {
        String str="helloworld";
        boolean boo=method(str,s->s.contains("a"),
                               s->s.length()>5);
        System.out.println(boo);
    }

    private static boolean method(String s, Predicate<String> one, Predicate<String> two) {
        return one.and(two).test(s);
    }
}
/*判断对象接口Predict<T>
默认方法default Predicate<T> or(Predicate<? super T> other)//有参有返回  一真为真
*/
//需求:如果要判断一个字符串既要包含字母“a”,或长度大于5。
public class Test06 {
    public static void main(String[] args) {
        String str="helloworld";
        boolean boo=method(str,s->s.contains("a"),
                s->s.length()>5);
        System.out.println(boo);
    }
    private static boolean method(String s, Predicate<String> one,Predicate<String> two) {
        return  one.or(two).test(s);
    }
}

/*判断对象接口Predict<T>
默认方法default Predicate<T> negate() {
        return (t) -> !test(t);
    }//无参,有返回
*/
//需求:如果要判断一个字符串是否包含字母“a”并取反。
public class Test07 {
    public static void main(String[] args) {
        String str="helloworld";
        boolean boo=method(str,s->s.contains("a"));
        System.out.println(boo);
    }
    private static boolean method(String s, Predicate<String> lambda) {
        return  lambda.negate().test(s);
    }
}

4.Function接口 用于类型转换 一个抽象一个默认

/*转换对象接口Function<T>
抽象方法:R apply(T t),有参有返回,将T类型转换为R类型
*///需求:"123"------>转换为123。
public class Test08 {
    public static void main(String[] args) {
        String str="123";
        int i = method(str,s->Integer.parseInt(s));
        System.out.println("i = " + i);
    }

    private static int method(String s, Function<String,Integer> lambda) {
        return lambda.apply(s);
    }
}
/*转换对象接口Function<T>
默认方法: default <V> Function<V, R> compose(Function<? super V, ? extends T> before),有参有返回,将V类型转换为R类型
*/
import java.util.function.Function;
/*
需求:给定字符串:String str = "赵丽颖,20";
1. 将字符串截取数字年龄部分,得到字符串;Function<String, String>
2. 将上一步的字符串转换成为int类型的数字;Function<String, Integer>
3. 将上一步的int数字加100,得到结果int数字。Function<Integer, Integer>
 */
public class Test09 {
    public static void main(String[] args) {
        String str="赵丽颖,20";
        int result =method(str,s->s.split(",")[1],s->Integer.parseInt(s),i->i+100);
        System.out.println(result);
    }
    private static int method(String str, Function<String,String> one,Function<String,Integer> two,Function<Integer,Integer> three) {
        return one.andThen(two).andThen(three).apply(str);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值