当前位置:网站首页>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));
}
}
边栏推荐
- Shell programming core technology "four"
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- 联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
- Oracle with as ORA-00903: invalid table name 多表报错
- 牛客小白月赛7 F题
- 1672. 最富有客户的资产总量
- In flinksql, in addition to data statistics, is the saved data itself a state
- 26. 删除有序数组中的重复项 C#解答
- 测试工程师如何“攻城”(下)
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
猜你喜欢
在线文本行固定长度填充工具
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Don't just learn Oracle and MySQL!
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
Oracle with as ORA-00903: invalid table name 多表报错
欧拉函数
node_exporter部署
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
Online text line fixed length fill tool
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
随机推荐
Hough Transform 霍夫变换原理
求2的n次方
生成XML元素
在线文本行固定长度填充工具
LM10丨余弦波动顺势网格策略
一文掌握数仓中auto analyze的使用
Caché WebSocket
Jetpack Compose 教程
SSRS筛选器的IN运算(即包含于)用法
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
How test engineers "attack the city" (Part I)
HDU 1097 A hard puzzle
Pytest 可视化测试报告之 Allure
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
Introduction to polyfit software
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
Build your own website (15)
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
Qt实现界面滑动切换效果
Master the use of auto analyze in data warehouse