当前位置:网站首页>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));
}
}
边栏推荐
- 读写关闭的channel是啥后果?
- Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
- 与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
- 安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
- PolyFit软件介绍
- 启牛开的证券账户安全吗?
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
- 1672. Total assets of the richest customers
- Go microservice (II) - detailed introduction to protobuf
- 在线SQL转Excel(xls/xlsx)工具
猜你喜欢
Stream流
Hough Transform 霍夫变换原理
A method of using tree LSTM reinforcement learning for connection sequence selection
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Introduction to polyfit software
欧拉函数
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
从实时应用角度谈通信总线仲裁机制和网络流控
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
Summary and sorting of 8 pits of redis distributed lock
随机推荐
Online sql to excel (xls/xlsx) tool
Stream流
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
From automation to digital twins, what can Tupo do?
在线SQL转Excel(xls/xlsx)工具
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
牛客小白月赛7 谁是神箭手
Shell 编程核心技术《二》
使用canal配合rocketmq监听mysql的binlog日志
Oracle with as ora-00903: invalid table name multi report error
Leetcode ransom letter C # answer
2019年蜀山区第十五届青少年信息学竞赛
HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
Lm10 cosine wave homeopathic grid strategy
每日一题(2022-07-02)——最低加油次数
Introduction to polyfit software
Leetcode fizzbuzz C # answer
The 300th weekly match of leetcode (20220703)
[release] a tool for testing WebService and database connection - dbtest v1.0
LeetCode第300场周赛(20220703)