当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢

The 300th weekly match of leetcode (20220703)

A method of using tree LSTM reinforcement learning for connection sequence selection

在线文本行固定长度填充工具

“只跑一趟”,小区装维任务主动推荐探索

Don't just learn Oracle and MySQL!

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

Nebula importer data import practice

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

Online sql to excel (xls/xlsx) tool

The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
随机推荐
SSRS筛选器的IN运算(即包含于)用法
prometheus安装
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
用实际例子详细探究OpenCV的轮廓绘制函数drawContours()
QT realizes interface sliding switching effect
Shell 编程核心技术《三》
The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
2014合肥市第三十一届青少年信息学奥林匹克竞赛(小学组)试题
明明的随机数
牛客小白月赛7 I 新建 Microsoft Office Word 文档
Introduction to polyfit software
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
1672. 最富有客户的资产总量
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
OpenCV的二值化处理函数threshold()详解
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
Pytorch学习(四)
26. Delete the duplicate item C solution in the ordered array
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
The kth largest element in the array