当前位置:网站首页>了解下常见的函数式接口
了解下常见的函数式接口
2022-06-26 15:57:00 【绅士jiejie】
Predicate
简介
该函数式接口主要是用于判断操作,内部有一个主要抽象方法以及一些默认方法,如下:
示例代码
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;
}
//会输出结果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
简介
该函数式接口主要是用于获取数据的操作,其内部方法定义如下:
示例代码
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);
}
}
//数字是1
//数字是2
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
test(list, (s) -> System.out.println("数字是" + s));
}
}
Function
简介
该函数式接口主要用于数据类型的转换,方法定义如下:
示例代码
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
简介
该函数式接口主要是用来值的获取的,但是和Consumer不同的是,Consumer有参数没返回值,Supplier
有返回值没入参,方法定义如下:
示例代码
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();
}
//返回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);
}
}
边栏推荐
- [time complexity and space complexity]
- Practice of federal learning in Tencent micro vision advertising
- redis的二进制数组命令
- JVM笔记
- NFT交易原理分析(1)
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
- 4 自定义模型训练
- STEPN 新手入門及進階
- R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
猜你喜欢
今年高考英语AI得分134,复旦武大校友这项研究有点意思
Svg capital letter a animation JS effect
JVM笔记
JVM notes
简单科普Ethereum的Transaction Input Data
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
NFT transaction principle analysis (2)
NFT Platform Security Guide (2)
随机推荐
Practice of federal learning in Tencent micro vision advertising
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
人人都当科学家之免Gas体验mint爱死机
NFT transaction principle analysis (1)
"C language" question set of ⑩
6 自定义层
IntelliJ idea -- Method for formatting SQL files
李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
2 three modeling methods
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
Golang temporary object pool optimization
JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
NFT Platform Security Guide (1)
R语言plotly可视化:plotly可视化归一化的直方图(historgram)并在直方图中添加密度曲线kde、并在直方图的底部边缘使用geom_rug函数添加边缘轴须图
固件供应链公司Binarly获得WestWave Capital和Acrobator Ventures的360万美元投资
5 模型保存与加载
Solana扩容机制分析(2):牺牲可用性换取高效率的极端尝试 | CatcherVC Research