当前位置:网站首页>函数式接口
函数式接口
2022-07-04 17:48:00 【Cold Snowflakes】
函数式接口
有且仅有一个抽象方法的接口,函数式接口就是 lambda 表达式的使用前提。
可以使用 @FunctionalInterface 进行标记 函数式接口。
使用函数式接口 作为方法参数时,可以传递过去一个 lambda 表达式。
public class lambdademo {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
array.add("ccc");
array.add("aa");
array.add("b");
array.add("dddd");
System.out.println("排序前: " + array);
// Collections.sort(array); // 自然排序
Collections.sort(array,getComparator()); // 比较器排序
System.out.println("排序后: " + array);
}
private static Comparator<String> getComparator(){
// 匿名内部类方式
// return new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length() - s2.length();
// }
// };
// lambda方式, 因为返回值是 函数式接口
return (s1,s2) -> s1.length() - s2.length();
}
}
常用的函数式接口
函数式接口,作方法的参数时,调用的时候实际传过去一个 lambda表达式就可以了。
Supplier接口
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Consumer接口
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
return (T t) -> {
accept(t); after.accept(t); };
}
}
getString("随便吧",System.out::print);
private static void getString(String s, Consumer<String> cn){
// cn.accept(s);
Consumer<String> t = a -> System.out.print(a.length() + 1);
cn.andThen(t).accept(s);
// cn先执行一次 accept(s)
// t 再执行一次 accept(s)
}
Predicate接口
常用于判断参数是否满足指定条件
boolean test(T t) // 对给定参数进行判断, 判断逻辑由 lambda 提供
default Predicate<T> negate()
default Predicate<T> and(Predicate other)
default Predicate<T> or(Predicate other)
public static void main(String[] args) {
boolean res = checkString("fewef", s -> s.length() > 8);
System.out.print(res);
}
private static boolean checkString(String s, Predicate<String> t){
// return t.test(s);
// return t.negate().test(s); // 非
// return t.and(a -> a.startsWith("fy")).test(s); // 与
return t.or(a -> a.startsWith("fy")).test(s); // 或
}
Function接口
Function接口用于对参数进行处理,转换,返回一个新的值。
@FunctionalInterface
public interface Function<T, R> {
// T 是函数输入的类型
R apply(T t); // R 是函数结果的类型
// 对输入 进行两次 逻辑处理
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
return (T t) -> after.apply(apply(t));
}
}
边栏推荐
猜你喜欢

Scala basic tutorial -- 13 -- advanced function

Angry bird design based on unity
Summary and sorting of 8 pits of redis distributed lock

A method of using tree LSTM reinforcement learning for connection sequence selection

2022年字节跳动日常实习面经(抖音)

How is the entered query SQL statement executed?

MXNet对GoogLeNet的实现(并行连结网络)

用实际例子详细探究OpenCV的轮廓绘制函数drawContours()

【uniapp】uniapp开发app在线预览pdf文件

Deleting nodes in binary search tree
随机推荐
php伪原创api对接方法
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
Mxnet implementation of googlenet (parallel connection network)
Unity editor extends C to traverse all pictures in folders and subdirectories
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
Deleting nodes in binary search tree
Torchdrug tutorial
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
2022年字节跳动日常实习面经(抖音)
模板_判断素数_开方 / 六素数法
Summary and sorting of 8 pits of redis distributed lock
redis分布式锁的8大坑总结梳理
Other InterSystems%net tools
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
Use canal and rocketmq to listen to MySQL binlog logs
C language printing exercise
Don't just learn Oracle and MySQL!
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
小发猫物联网平台搭建与应用模型