当前位置:网站首页>函数式接口
函数式接口
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));
}
}
边栏推荐
- Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
- 每日一题(2022-07-02)——最低加油次数
- node_exporter部署
- 2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
- MXNet对GoogLeNet的实现(并行连结网络)
- 模板_判断素数_开方 / 六素数法
- Caché WebSocket
- LeetCode 赎金信 C#解答
- Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
- 自由小兵儿
猜你喜欢

LeetCode第300场周赛(20220703)
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file
![[release] a tool for testing WebService and database connection - dbtest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[release] a tool for testing WebService and database connection - dbtest v1.0

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

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

Bi skills - permission axis

Go微服务(二)——Protobuf详细入门

Scala基础教程--17--集合

Lex and yacc based lexical analyzer + parser

What if the self incrementing ID of online MySQL is exhausted?
随机推荐
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
2021 合肥市信息学竞赛小学组
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
The 15th youth informatics competition in Shushan District in 2019
Nebula importer data import practice
Lex and yacc based lexical analyzer + parser
OpenCV的二值化处理函数threshold()详解
1672. 最富有客户的资产总量
工厂从自动化到数字孪生,图扑能干什么?
【机器学习的数学基础】(一)线性代数(Linear Algebra)(上+)
SSL证书续费相关问题详解
Perfect JS event delegation
Li Chi's work and life summary in June 2022
自由小兵儿
A method of using tree LSTM reinforcement learning for connection sequence selection
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
LeetCode第300场周赛(20220703)
Use canal and rocketmq to listen to MySQL binlog logs
Leetcode fizzbuzz C # answer
Scala basic tutorial -- 12 -- Reading and writing data