Function系列函数式接口主要作用是表示的是参数类型和其结果返回类型不一致。
比如:
public class TestFunction {
public static void main(String[] args) {
testApply("test", val -> val.length());
}
static void testApply(String str, Function<String, Integer> function) {
Integer apply = function.apply(str);
System.out.println(apply);
}
}
输出结果是:4。表示传入的参数是字符串类型的“test”,返回的是其长度为:4。
Function类似的接口在JDK8中提供了17个不同使用类型的接口,分别:
目录
-
Function
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; } }
可以看到Function接口中有四个函数,这四个方法很重要,其中只有一个抽象方法apply()。
R apply(T t) :将Function函数对象应用于输入的参数上,并返回计算的结果。
代码示例
public class TestFunction { public static void main(String[] args) { // 计算字符串的长度 testApply("test", val -> val.length()); // 用空格分隔字符串,并计算分割后数组的长度 testApply("test function demo", val -> val.split(" ").length); } public static void testApply(String str, Function<String, Integer> function) { Integer apply = function.apply(str); System.out.println(apply); } }
输出结果:
4 3
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) :先执行before函数的apply()方法,然后再执行其返回的Function对象的apply()方法。
示例代码
public class TestFunction { public static void main(String[] args) { // 将字符串转为大写并加上“Compose”,然后计算拼接后的字符串的长度 testCompose("test", str -> str.toUpperCase() + "Compose", str -> str.length()); // 将Integer类型的123和字符串拼“testCompose”接起来,然后将其转为大写 testCompose(Integer.valueOf("123"), val -> "testCompose(" + val + ")", str -> str.toUpperCase()); } public static void testCompose(String str, Function<String, String> before, Function<String, Integer> function) { Function<String, Integer> compose = function.compose(before); Integer apply = compose.apply(str); System.out.println(apply); } public static void testCompose(Integer val, Function<Integer, String> before, Function<String, String> function) { Function<Integer, String> compose = function.compose(before); String apply = compose.apply(val); System.out.println(apply); } }
此时需要注意的是testCompose()方法中before和function参数的泛型,before的输出泛型(String)必须和function的输入泛型(String)一致。或者function的输入类型为before的子类。输出结果
11 TESTCOMPOSE(123)
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) :先执行当前Function函数对象的apply()方法,再执行after函数对象的apply()方法。
示例代码:
public class TestFunction { public static void main(String[] args) { // 将Integer类型的666和字符串类型的888拼接起来,然后转为Integer类型并乘以2 testAndThen(Integer.valueOf("666"), val -> String.valueOf(val), str -> Integer.valueOf(str + "888") * 2); } public static void testAndThen(Integer val, Function<Integer, String> function, Function<String, Integer> after) { Function<Integer, Integer> andThen = function.andThen(after); Integer apply = andThen.apply(val); System.out.println(apply); // 结果:1333776 } }
同时也需要注意他们的两个泛型限制。
static <T> Function<T, T> identity() :永远返回输入的参数值。
-
BiFunction
表示接受两个参数并生成结果的函数。 参考源码:-
/** * T 和 U 是输入的两个参数,R 是返回的值。 */ @FunctionalInterface public interface BiFunction<T, U, R> { R apply(T t, U u); default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t, U u) -> after.apply(apply(t, u)); } }
比如:
public class TestBiFunction { public static void main(String[] args) { test("abc", "123", (str1, str2) -> (str1 + str2).length()); test(new User("张三", 20, "北京"), (str1, str2) -> str1.length() + str2.length()); } public static void test(String str1, String str2, BiFunction<String, String, Integer> function) { Integer apply = function.apply(str1, str2); System.out.println(apply); } public static void test(User user, BiFunction<String, String, Integer> function) { Integer apply = function.apply(user.getName(), user.getAddress()); System.out.println(apply); } }
输出结果
6 4
-
-
IntFunction
表示接收一个Int类型的参数,返回一个<R>类型的值,里面只有一个方法:@FunctionalInterface public interface IntFunction<R> { R apply(int value); }
示例:
public class TestIntFunction { public static void main(String[] args) { test1(10, val -> val * 5); test2(10, val -> val + "aaa"); } public static void test1(int val, IntFunction<Integer> function) { Integer apply = function.apply(val); System.out.println(apply); } public static void test2(int val, IntFunction<String> function) { String apply = function.apply(val); System.out.println(apply); } } // 结果如下: // 50 // 10aaa
-
LongFunction
同IntFunction一样,接收一个Long类型的参数。返回一个泛型中的值。 -
DoubleFunction
同IntFunction一样,接收一个Double类型的参数。返回一个泛型中的值。 -
IntToLongFunction
表示接收一个Int类型的参数,返回Long类型的值。 源码:public interface IntToLongFunction { /** * Applies this function to the given argument. * * @param value the function argument * @return the function result */ long applyAsLong(int value); }
代码示例:
public class TestIntToLongFunction { public static void main(String[] args) { test(100, val -> val * 2); } public static void test(int val, IntToLongFunction function) { long apply = function.applyAsLong(val); System.out.println(apply); } }
-
IntToDoubleFunction
同IntToLongFunction用法一样,这个表示接收一个Int类型的参数,返回一个Double类型的值。 -
LongToIntFunction
表示接收一个Long类型的参数,返回一个Int类型的值。用法同IntToLongFunction 。 -
LongToDoubleFunction
表示接收一个Long类型的参数,返回一个Double类型的值。用法同IntToLongFunction 。 -
DoubleToIntFunction
表示接收一个Double类型的参数,返回一个Int类型的值。用法同IntToLongFunction 。 -
DoubleToLongFunction
表示接收一个Double类型的参数,返回一个Long类型的值。用法同IntToLongFunction 。 -
ToIntFunction
@FunctionalInterface public interface ToIntFunction<T> { /** * Applies this function to the given argument. * * @param value the function argument * @return the function result */ int applyAsInt(T value); }
表示接收一个<T>类型的参数,返回一个Int类型的值。示例:
public class TestToIntFunction { public static void main(String[] args) { test("123456789", str -> Integer.valueOf(str)); User user = new User("张三", 23, "北京"); test(user, obj -> obj.getAge()); // 标识1 test(user, User::getAge); // 标识2 } public static void test(String str, ToIntFunction<String> function) { int apply = function.applyAsInt(str); System.out.println(apply); } public static void test(User user, ToIntFunction<User> function) { int apply = function.applyAsInt(user); System.out.println("年龄:" + apply); } }
在此示例代码中,标识1和标识2的意思一样,只不过使用方式不同。
控制台输出结果:123456789 年龄:23 年龄:23
-
ToIntBiFunction
表示接收两个参数,分别是<T>和<U>,并且返回一个Int类型的值。示例代码:public interface ToIntBiFunction<T, U> { /** * Applies this function to the given arguments. * * @param t the first function argument * @param u the second function argument * @return the function result */ int applyAsInt(T t, U u); }
public class TestToIntBiFunction { public static void main(String[] args) { // 将"123"乘以10 test("123", 10, (str, val) -> Integer.valueOf(str) * val); List<String> list = new ArrayList<>(); list.add("张三"); list.add("李四"); list.add("王五"); User user = new User("王五", 30, "上海"); // 找出王五这个人的姓名在List集合中的位置(下标)。 test(list, user, (names, e) -> { for (int i = 0; i < names.size(); i++) { if (e.getName().equals(names.get(i))) { return i; } } return -1; }); } public static void test(String str, int val, ToIntBiFunction<String, Integer> function) { int apply = function.applyAsInt(str, val); System.out.println(apply); } public static void test(List<String> list, User user, ToIntBiFunction<List<String>, User> function) { int apply = function.applyAsInt(list, user); System.out.println(apply); } }
控制台输出结果
1230 2
-
ToLongFunction
表示接收一个<T>类型的参数,返回一个Long类型的值。用法参考ToIntFunction。 -
ToLongBiFunction
表示接收两个参数,分别是<T>和<U>,并且返回一个Long类型的值。用法同ToIntBiFunction。 -
ToDoubleFunction
表示接收一个<T>类型的参数,返回一个Double类型的值。用法参考ToIntFunction。 -
ToDoubleBiFunction
表示接收两个参数,分别是<T>和<U>,并且返回一个Double类型的值。用法同ToIntBiFunction。