当前位置:网站首页>Learn about common functional interfaces
Learn about common functional interfaces
2022-06-26 16:13:00 【Gentleman jiejie】
Predicate
brief introduction
This functional interface is mainly used to judge the operation , There is a main abstract method and some default methods inside , as follows :
Sample code
package org.example.basicskills.functionalinterface;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class PredicateDemo {
public static List<String> test(List<String> list, Predicate<String> predicate) {
List<String> list2 = new ArrayList<>();
for (String s : list) {
if (predicate.test(s)) {
list2.add(s);
}
}
return list2;
}
// Will output the result 2
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
List<String> result = test(list, (s) -> s.equals("2"));
result.forEach(item -> System.out.println(item));
}
}
Consumer
brief introduction
This functional interface is mainly used to obtain data , Its internal methods are defined as follows :
Sample code
package org.example.basicskills.functionalinterface;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerDemo {
public static void test(List<String> list, Consumer<String> consumer) {
List<String> list2 = new ArrayList<>();
for (String s : list) {
consumer.accept(s);
}
}
// The number is 1
// The number is 2
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
test(list, (s) -> System.out.println(" The number is " + s));
}
}
Function
brief introduction
This functional interface is mainly used for data type conversion , The method is defined as follows :

Sample code
package org.example.basicskills.functionalinterface;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class FunctionDemo {
public static List<Integer> test(List<String> list, Function<String, Integer> function) {
List<Integer> list2 = new ArrayList<>();
for (String s : list) {
Integer result = function.apply(s);
list2.add(result);
}
return list2;
}
//true
//true
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
List<Integer> list2 = test(list, (s) -> Integer.parseInt(s));
list2.forEach(s -> System.out.println(s instanceof Integer));
}
}
Supplier
brief introduction
This functional interface is mainly used to obtain values , But and Consumer The difference is ,Consumer There are parameters without return value ,Supplier
No input parameter with return value , The method is defined as follows :
Sample code
package org.example.basicskills.functionalinterface;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class SupplierDemo {
public static String test(List<String> list, Supplier<String> supplier) {
return supplier.get();
}
// return 2
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
String result = test(list, () -> {
return list.get(1);
});
System.out.println(result);
}
}
边栏推荐
- H5 close the current page, including wechat browser (with source code)
- 8 user defined evaluation function
- Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock
- (DFS search) acwing 2005 horseshoe
- 李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...
- Failed to upload hyperf framework using alicloud OSS
- 5000 word analysis: the way of container security attack and defense in actual combat scenarios
- Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
- Net based on girdview control to delete and edit row data
- JVM notes
猜你喜欢
Practice of federal learning in Tencent micro vision advertising

Stepn novice introduction and advanced

SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)

『C语言』题集 of ⑩

1-12vmware adds SSH function

10 tf. data

How to implement interface current limiting?

100+ data science interview questions and answers Summary - basic knowledge and data analysis

2 three modeling methods

用Attention和微调BERT进行自然语言推断-PyTorch
随机推荐
Interview pit summary I
C# 读写文件从用户态切到内核态,到底是个什么流程?
人人都当科学家之免Gas体验mint爱死机
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
Transformation of zero knowledge QAP problem
2 三种建模方式
Handwritten numeral recognition, run your own picture with the saved model
NFT合约基础知识讲解
Everyone is a scientist free gas experience Mint love crash
手写数字体识别,用保存的模型跑自己的图片
5 模型保存与加载
【小5聊】毕业8年,一直在追梦的路上
Keepalived 实现 Redis AutoFailover (RedisHA)
JS text scrolling scattered animation JS special effect
Hyperf框架使用阿里云OSS上传失败
3 keras版本模型训练
01 backpack DP
2 three modeling methods
Redis的ACID
[207] several possible causes of Apache crash