当前位置:网站首页>函数式接口
函数式接口
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));
}
}
边栏推荐
- Rookie post station management system based on C language
- LeetCode FizzBuzz C#解答
- Caché JSON 使用JSON适配器
- 2019年蜀山区第十五届青少年信息学竞赛
- 1672. Total assets of the richest customers
- Wireshark packet capturing TLS protocol bar displays version inconsistency
- 神经网络物联网应用技术就业前景【欢迎补充】
- 千万不要只学 Oracle、MySQL!
- Pb extended DLL development (super chapter) (VII)
- The 300th weekly match of leetcode (20220703)
猜你喜欢
Scala基础教程--17--集合
Scala basic tutorial -- 19 -- actor
整理混乱的头文件,我用include what you use
Principle and application of ThreadLocal
使用canal配合rocketmq监听mysql的binlog日志
升级智能开关,“零火版”、“单火”接线方式差异有多大?
神经网络物联网应用技术学什么
Use canal and rocketmq to listen to MySQL binlog logs
Angry bird design based on unity
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
随机推荐
A method of using tree LSTM reinforcement learning for connection sequence selection
6.26cf simulation match B: solution to array reduction problem
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
2022健康展,北京健博会,中国健康展,大健康展11月13日
One question per day (2022-07-02) - Minimum refueling times
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Use canal and rocketmq to listen to MySQL binlog logs
Go微服务(二)——Protobuf详细入门
26. 删除有序数组中的重复项 C#解答
The 300th weekly match of leetcode (20220703)
删除字符串中出现次数最少的字符【JS,Map排序,正则】
876. 链表的中间结点
[uniapp] uniapp development app online Preview PDF file
神经网络物联网是什么意思通俗的解释
IBM WebSphere MQ retrieving messages
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
升级智能开关,“零火版”、“单火”接线方式差异有多大?
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
2022CoCa: Contrastive Captioners are Image-Text Fountion Models