当前位置:网站首页>Functional interface
Functional interface
2022-07-04 19:33:00 【Cold Snowflakes】
Functional interface
Interface with and only one abstract method , A functional interface is lambda The premise of expression .
have access to @FunctionalInterface marked Functional interface .
Using functional interfaces As a method parameter , You can pass on the past lambda expression .
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(" Before ordering : " + array);
// Collections.sort(array); // Natural ordering
Collections.sort(array,getComparator()); // Comparator sort
System.out.println(" After ordering : " + array);
}
private static Comparator<String> getComparator(){
// Anonymous inner class mode
// return new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length() - s2.length();
// }
// };
// lambda The way , Because the return value is Functional interface
return (s1,s2) -> s1.length() - s2.length();
}
}
Common functional interfaces
Functional interface , When making parameters of the method , When calling, it actually passes a lambda The expression is OK .
Supplier Interface
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Consumer Interface
@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(" Xy: ",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 Do it first accept(s)
// t Execute it again accept(s)
}
Predicate Interface
It is often used to judge whether parameters meet specified conditions
boolean test(T t) // Judge the given parameters , The logic of judgment consists of lambda Provide
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); // Not
// return t.and(a -> a.startsWith("fy")).test(s); // And
return t.or(a -> a.startsWith("fy")).test(s); // or
}
Function Interface
Function Interface is used to process parameters , transformation , Returns a new value .
@FunctionalInterface
public interface Function<T, R> {
// T Is the type of function input
R apply(T t); // R Is the type of function result
// For input Do it twice Logical processing
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
return (T t) -> after.apply(apply(t));
}
}
边栏推荐
- 26. 删除有序数组中的重复项 C#解答
- C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
- 国元期货是正规平台吗?在国元期货开户安全吗?
- SSL证书续费相关问题详解
- Oracle with as ORA-00903: invalid table name 多表报错
- 欧拉函数
- One question per day (2022-07-02) - Minimum refueling times
- 矩阵翻转(数组模拟)
- Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
- Shell programming core technology "four"
猜你喜欢

英特尔集成光电研究最新进展推动共封装光学和光互连技术进步

勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
牛客小白月赛7 谁是神箭手

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总

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

Bi skills - permission axis

Build your own website (15)

Nebula Importer 数据导入实践

整理混乱的头文件,我用include what you use

One question per day (2022-07-02) - Minimum refueling times
随机推荐
Don't just learn Oracle and MySQL!
Oracle with as ORA-00903: invalid table name 多表报错
Summary and sorting of 8 pits of redis distributed lock
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
In flinksql, in addition to data statistics, is the saved data itself a state
测试工程师如何“攻城”(上)
Wireshark网络抓包
The kth largest element in the array
Pytest 可视化测试报告之 Allure
求2的n次方
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
redis分布式锁的8大坑总结梳理
Qt实现界面滑动切换效果
26. Delete the duplicate item C solution in the ordered array
Have you guys ever used CDC direct Mysql to Clickhouse
明明的随机数
大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
反射(一)
1672. 最富有客户的资产总量