当前位置:网站首页>函数式接口
函数式接口
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基础教程--16--泛型
- 2021 合肥市信息学竞赛小学组
- [release] a tool for testing WebService and database connection - dbtest v1.0
- IBM WebSphere MQ retrieving messages
- 876. Intermediate node of linked list
- redis分布式锁的8大坑总结梳理
- 请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
- Scala basic tutorial -- 12 -- Reading and writing data
- Li Chi's work and life summary in June 2022
- 爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
猜你喜欢

Scala basic tutorial -- 18 -- set (2)

Deleting nodes in binary search tree

Torchdrug tutorial

Wireshark网络抓包

神经网络物联网是什么意思通俗的解释

小发猫物联网平台搭建与应用模型
Summary and sorting of 8 pits of redis distributed lock

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Scala基础教程--19--Actor

用实际例子详细探究OpenCV的轮廓绘制函数drawContours()
随机推荐
神经网络物联网是什么意思通俗的解释
Scala基础教程--14--隐式转换
DeFi生态NFT流动性挖矿系统开发搭建
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
From automation to digital twins, what can Tupo do?
Lex and yacc based lexical analyzer + parser
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Scala basic tutorial -- 20 -- akka
C language printing exercise
启牛开的证券账户安全吗?
Caché WebSocket
Use canal and rocketmq to listen to MySQL binlog logs
页面元素垂直水平居中、实现已知或者未知宽度的垂直水平居中。
Is the securities account opened by qiniu safe?
【uniapp】uniapp开发app在线预览pdf文件
国元期货是正规平台吗?在国元期货开户安全吗?
Scala basic tutorial -- 12 -- Reading and writing data
物联网应用技术的就业前景和现状