当前位置:网站首页>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);
}
}
边栏推荐
- JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
- Solana capacity expansion mechanism analysis (2): an extreme attempt to sacrifice availability for efficiency | catchervc research
- 国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
- Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
- Ideal path problem
- 【蓝桥杯集训100题】scratch辨别质数合数 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第15题
- Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
- How to identify contractual issues
- redis的二进制数组命令
- 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
猜你喜欢

基于STM32+华为云IOT设计的云平台监控系统

9 use of tensorboard

Handwritten numeral recognition, run your own picture with the saved model

Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo

Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl

Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law

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

Angel 3.2.0 new version released! Figure the computing power is strengthened again

Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统

1 张量的简单使用
随机推荐
5 模型保存与加载
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
数据分析----numpy快速入门
8 user defined evaluation function
精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
【力扣刷题】单调栈:84. 柱状图中最大的矩形
用Attention和微调BERT进行自然语言推断-PyTorch
10 tf.data
若依打包如何分离jar包和资源文件?
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
Canvas three dot flashing animation
Acid of redis
6 自定义层
Net based on girdview control to delete and edit row data
01 backpack DP
架构实战营毕业设计
人人都当科学家之免Gas体验mint爱死机
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul