首页 技术 正文
技术 2022年11月19日
0 收藏 376 点赞 2,939 浏览 3589 个字

这篇文章属于 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]

参考

扩展阅读

订阅

可以关注未读代码博客或者微信搜索「 未读代码 」。

文章会在博客和公众号同步更新。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893