Java 8 Function 函数接口
点赞再看,动力无限。Hello world : ) 微信搜「 程序猿阿朗 」。
本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章。
这篇文章属于 Java 8 教程(LTS)系列教程
在 Java 8 中,Function 接口是一个函数接口,它位于包 java.util.function
下。 Function
接口中定义了一个 R apply(T t)
方法,它可以接受一个泛型 T 对象,返回一个泛型 R 对象,即参数类型和返回类型可以不同。
Function 接口源码:
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
1. Function apply
示例 1:输入一个字符串 <T> String
, 返回字符串的大写形式 <R> String
。
package com.wdbyte;
import java.util.function.Function;
public class Java8Function {
public static void main(String[] args) {
Function<String, String> toUpperCase = str -> str.toUpperCase();
String result = toUpperCase.apply("www.wdbyte.com");
System.out.println(result);
}
}
输出结果:
WWW.WDBYTE.COM
示例 2:输入一个字符串 <T> String
,返回字符串的长度 <R> Integer
。
package com.wdbyte;
import java.util.function.Function;
public class Java8FunctionLength {
public static void main(String[] args) {
Function<String, Integer> lengthFunction = str -> str.length();
Integer length = lengthFunction.apply("www.wdbyte.com");
System.out.println(length);
}
}
输出结果:
14
2. Function andThen
Function 函数接口的 andThen()
方法可以让多个 Function 函数连接使用。
示例:输入一个字符串,获取字符串的长度,然后乘上 2。
package com.wdbyte;
import java.util.function.Function;
public class Java8FunctionAndThen {
public static void main(String[] args) {
Function<String, Integer> lengthFunction = str -> str.length();
Function<Integer, Integer> doubleFunction = length -> length * 2;
Integer doubleLength = lengthFunction.andThen(doubleFunction).apply("www.wdbyte.com");
System.out.println(doubleLength);
}
}
结果:
28
3. List -> Map
示例:输入一个字符串 List 集合 <T> List<String>
, 返回一个 key 为字符串本身,Value 为字符串长度的 Map
。
package com.wdbyte;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class Java8FunctionListToMap {
public static void main(String[] args) {
List<String> list = Arrays.asList("java", "nodejs", "wdbyte.com");
// lambda 方式
Function<String, Integer> lengthFunction = str -> str.length();
Map<String, Integer> listToMap = listToMap(list, lengthFunction);
System.out.println(listToMap);
// 方法引用方式
Map<String, Integer> listToMap2 = listToMap(list, String::length);
System.out.println(listToMap2);
}
public static <T, R> Map<T, R> listToMap(List<T> list, Function<T, R> function) {
HashMap<T, R> hashMap = new HashMap<>();
for (T t : list) {
hashMap.put(t, function.apply(t));
}
return hashMap;
}
}
输出结果:
{java=4, wdbyte.com=10, nodejs=6}
{java=4, wdbyte.com=10, nodejs=6}
4. List -> List<Other>
示例 :输入一个字符串 List
集合 <T> List<String>
,返回大写形式的字符串 List
集合,返回小写形式的字符串 List
集合。
package com.wdbyte;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class Java8FunctionString {
public static void main(String[] args) {
List<String> list = Arrays.asList("Java", "Nodejs", "Wdbyte.com");
// 方法引用方式
List<String> upperList = map(list, String::toUpperCase);
List<String> lowerList = map(list, String::toLowerCase);
System.out.println(upperList);
System.out.println(lowerList);
// Lambda 方式
List<String> upperList2 = map(list, x -> x.toUpperCase());
List<String> lowerList2 = map(list, x -> x.toLowerCase());
System.out.println(upperList2);
System.out.println(lowerList2);
}
public static <T, R> List<R> map(List<T> list, Function<T, R> function) {
List<R> resultList = new ArrayList<>(list.size());
for (T t : list) {
resultList.add(function.apply(t));
}
return resultList;
}
}
输出结果:
[JAVA, NODEJS, WDBYTE.COM]
[java, nodejs, wdbyte.com]
[JAVA, NODEJS, WDBYTE.COM]
[java, nodejs, wdbyte.com]
参考
扩展阅读
- Java 11 新特性介绍
- Java 10 新特性介绍
- Java 09 新特性介绍
- Java 8 新特性 - Stream 介绍
- Java 8 新特性 - Lambda 表达式介绍
- Java 8 新特性 - 新的时间处理方式
- Java 8 新特性 - 使用Optional处理空指针
- Java 7 新特性介绍
- Java 7 新特性 - NIO.2 介绍
订阅
<完>
Hello world : ) 我是阿朗,一线技术工具人,认认真真写文章。
点赞的个个都是人才,不仅长得帅气好看,说话还好听。
文章持续更新,可以关注公众号「 程序猿阿朗 」或访问「未读代码博客 」。
回复【资料】有我准备的各系列知识点和必看书籍。
本文 Github.com/niumoo/JavaNotes 已经收录,有很多知识点和系列文章,欢迎Star。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端