当前位置:网站首页>Functional interface lambda, elegant code development
Functional interface lambda, elegant code development
2022-06-11 05:52:00 【Endwas】
Welcome to my blog , communication :https://endwas.cn
JDK Now there are many Functional Interface This feature is used to replace our traditional anonymous inner class writing ,
Traditionally we use
- Instantiation interface
- Rewrite the corresponding method in the interface
To help us achieve , But the code of this method is miscellaneous , And it doesn't look good .
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
}
});
therefore Java8 Added Functional Interface Functional interface , His characteristic is that there is only one abstract method , This feature helps us when transferring parameters , The compiler will know which method we are implementing , Write according to the corresponding format lambda The expression is OK , Development is easy to understand .
I believe you still don't know what the use is , I can't understand , It doesn't matter. Let's take an example
1. Definition and use
This interface you need to determine ,1. Whether parameters are required , Is a parameter of any type ( Generic ) Or a specific type ;2. Whether to return a value , Whether the return value is of any type or fixed, such as boolean;
- Defined a Animal Interface , Enter the reference String, Return value void, When we use this method, we should follow this standard !
@FunctionalInterface
public interface Animal {
void eat(String food);
}
- Methods defined lunch, You need to pass in the above Functional Interface As the reference ;
- stay lunch Inside the method body Anywhere you want Call the eat Abstract method ;
static void lunch(Animal animal) {
System.out.println("animal is eating");
animal.eat("hao chi!");
System.out.println("animal have had lunch");
}
- So we can use lambda Expression to implement the specific interface method ;
- Follow the abstract method specification : A into the refs , We define this input parameter as message, No return value means no return;
public class main {
public static void main(String[] args) {
lunch(message -> {
message += " Have a big meal ";
System.out.println(message);
});
}
}
2.JDK Functional interface defined in
1. Runnable class
characteristic : Without the participation , No return value
@FunctionalInterface
public interface Runnable {
void run();
}
Runnable r = ()->{
System.out.println("this is runnable interface");
};
new Thread(r).start();
2. Callable class
characteristic : Without the participation , There is a return value V
@FunctionalInterface
public interface Callable<V> {
V call() throws Exception;
}
Callable c = ()-> "callable.class";
Future submit = Executors.newSingleThreadExecutor().submit(c);
Optional.of(submit).ifPresent(o->{
try {
System.out.println(o.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
3.Function class
characteristic : Ginseng with entry T, There is a return value R
@FunctionalInterface
public interface Function<T, R> {
R apply(T var1);
}
4.Consumer class
characteristic : Ginseng with entry T, No return value
@FunctionalInterface
public interface Consumer<T> {
void accept(T var1);
}
5.Supplier class
characteristic : Without the participation , There is a return value T
@FunctionalInterface
public interface Supplier<T> {
T get();
}
6.Predict class
characteristic : Ginseng with entry T, There is a return value boolean
@FunctionalInterface
public interface Predicate<T> {
boolean test(T var1);
}
| The interface name | Input type | The output type | The core approach | explain |
|---|---|---|---|---|
| Consumer[T] | T | void | accept(T) | Consumer |
| Supplier[T] | void | T | T get() | Supply type |
| Function[T,R] | T | R | R apply(T) | functional |
| Predicate[T] | T | boolean | boolean test(T) | Judgmental |
Roughly according to the type, there are these kinds of ,Function There are also some extensions inside the package , such as DoubleConsumer, The input parameter changes from generic to double etc. ;
summary :
Functional Interface Help us start functional programming , In many packages, corresponding use has been added , such as list Of forEach、Stream Bag, etc 、Optional Packages and other internal packages use functional interfaces as methods , Let's call the logic we want inside the method body ;
边栏推荐
- ReferenceError: server is not defined
- 深度学习分布式训练
- Use of vlayout
- Further efficient identification of memory leakage based on memory optimization tool leakcanary and bytecode instrumentation technology
- What is a thread pool?
- 【深入kotlin】 - 初识 Flow
- Informatica: six steps of data quality management
- SQLite one line SQL implementation updates if there is one, inserts if there is none, multiple conditions, complex conditions
- Sword finger offer 50: the first character that appears only once
- SwiftUI: Navigation all know
猜你喜欢

NDK learning notes (14) create an avi video player using avilib+window

20多种云协作功能,3分钟聊透企业的数据安全经

Maximum number of points on the line ----- hash table solution

Wechat applet learning record

All questions and answers of database SQL practice niuke.com

What happened to the young man who loved to write code -- approaching the "Yao Guang young man" of Huawei cloud

Sword finger offer 50: the first character that appears only once

Wechat custom component - style - slot

安装Oracle数据库

Yonghong Bi product experience (I) data source module
随机推荐
How to deal with message blackout and message sending failure of Aurora im
做亚马逊测评要了解的知识点有哪些?
[go deep into kotlin] - get to know flow for the first time
11. Gesture recognition
Further efficient identification of memory leakage based on memory optimization tool leakcanary and bytecode instrumentation technology
Bert knowledge distillation
数据接入平台方案实现(游族网络)
使用Batch枚舉文件
[metadata]linkedin datahub
NDK R21 compiles ffmpeg 4.2.2+x264 and converts video files using ffmpeg
Delegation agreement, data source agreement and advanced view in view
SwiftUI: Navigation all know
NDK learning notes (IX) POSIX sockect connection oriented communication
Data quality: the core of data governance
JS -- reference type
修复Yum依赖冲突
Vscode plug-in development
qmake 实现QT工程pro脚本转vs解决方案
Implementation of data access platform scheme (Youzu network)
Informatica: six steps of data quality management