当前位置:网站首页>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));
}
}
边栏推荐
- Bi skills - permission axis
- node_exporter部署
- Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
- 1002. A+B for Polynomials (25)(PAT甲级)
- An example of multi module collaboration based on NCF
- SSL证书续费相关问题详解
- 1008 Elevator(20 分)(PAT甲级)
- 26. 删除有序数组中的重复项 C#解答
- 请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
- One question per day (2022-07-02) - Minimum refueling times
猜你喜欢
BI技巧丨权限轴
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
Hough transform Hough transform principle
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
To sort out messy header files, I use include what you use
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
[uniapp] uniapp development app online Preview PDF file
A method of using tree LSTM reinforcement learning for connection sequence selection
每日一题(2022-07-02)——最低加油次数
Wireshark网络抓包
随机推荐
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
Master the use of auto analyze in data warehouse
使用canal配合rocketmq监听mysql的binlog日志
Shell programming core technology "three"
Hough transform Hough transform principle
A method of using tree LSTM reinforcement learning for connection sequence selection
LeetCode 赎金信 C#解答
1672. 最富有客户的资产总量
1005 Spell It Right(20 分)(PAT甲级)
Detailed explanation of the binary processing function threshold() of opencv
在线SQL转Excel(xls/xlsx)工具
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
牛客小白月赛7 I 新建 Microsoft Office Word 文档
欧拉函数
FPGA timing constraint sharing 01_ Brief description of the four steps
神经网络物联网平台搭建(物联网平台搭建实战教程)
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
Nebula Importer 数据导入实践
测试工程师如何“攻城”(上)
26. 删除有序数组中的重复项 C#解答