java8 Funciton和BiFunction都是功能性接口,Function包含一个入参(T),一个出参(R),输入输出可以是不不同的类型,BiFunction包含两个入参T和R,一个出参R。
1、Function<T, R>
下面这个例子展示了一个输入参数<T> String类型,然后输出一个<R> Integer类型
package com.cattles.function;
import java.util.function.Function;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/1 下午5:40
*/
public class Java8Function1 {
public static void main(String[] args) {
Function<String, Integer> func = x -> x.length();
Integer apply = func.apply("cattle");
System.out.println(apply);
}
}
输出:
6
2、Function<T, R> addThen和compose
package com.cattles.function;
import java.util.function.Function;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/1 下午5:45
*/
public class Java8Function2 {
public static void main(String[] args) {
Function<String, Integer> func1 = x -> x.length();
Function<Integer, Integer> func2 = x -> x * 2;
Integer ret = func1.andThen(func2).apply("cattle");
System.out.println(ret);
Integer ret2 = func2.compose(func1).apply("cattle");
System.out.println(ret2);
}
}
输出:
12
12
问题:思考addThen和compose的区别
3、Function复杂一些的例子
package com.cattles.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.function.Function;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/1 下午10:43
*/
public class Java8Function3 {
public static void main(String[] args) {
Java8Function3 obj = new Java8Function3();
List<String> list = Arrays.asList("java", "python", "C#", "PHP", "javascript");
List<String> ret = obj.list(list, obj::base64);
ret.forEach(System.out::println);
}
public <T, R> List<R> list(List<T> list, Function<T, R> function) {
List<R> ret = new ArrayList<>();
for (T t : list) {
ret.add(function.apply(t));
}
return ret;
}
public String base64(String st) {
return Base64.getEncoder().encodeToString(st.getBytes());
}
}
输出:
amF2YQ==
cHl0aG9u
QyM=
UEhQ
amF2YXNjcmlwdA==
4、BiFunction<T, U, R>
package com.cattles.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/1 下午10:54
*/
public class Java8BiFunction1 {
public static void main(String[] args) {
BiFunction<Integer, Integer, Integer> func = (T, U) -> T * U;
System.out.println(func.apply(3, 4));
Java8BiFunction1 obj = new Java8BiFunction1();
List<String> list1 = Arrays.asList("a", "b", "c");
List<Integer> list2 = Arrays.asList(1, 2, 3);
List<String> result = obj.listCombiner(list1, list2, (a, b) -> a + b);
System.out.println(result);
}
private <T, U, R> List<R> listCombiner(
List<T> list1, List<U> list2, BiFunction<T, U, R> combiner) {
List<R> result = new ArrayList<>();
for (int i = 0; i < list1.size(); i++) {
result.add(combiner.apply(list1.get(i), list2.get(i)));
}
return result;
}
}
输出:
12
[a1, b2, c3]
5、BiFunction<T, U, R> addThen
package com.cattles.function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/1 下午11:02
*/
public class Java8BiFunction2 {
public static void main(String[] args) {
BiFunction<String, String, String> func1 = (T, U) -> T + U;
Function<String, String> func2 = x -> x + " Java";
System.out.println(func1.andThen(func2).apply("I", " Love"));
List<String> list1 = Arrays.asList("a", "b", "c");
List<String> list2 = Arrays.asList("1", "2", "3");
Java8BiFunction2 obj = new Java8BiFunction2();
List<String> result = obj.listCombiner(list1, list2, String::concat);
System.out.println(result);
}
private <T, U, R> List<R> listCombiner(
List<T> list1, List<U> list2, BiFunction<T, U, R> combiner) {
List<R> result = new ArrayList<>();
for (int i = 0; i < list1.size(); i++) {
result.add(combiner.apply(list1.get(i), list2.get(i)));
}
return result;
}
}
输出:
I Love Java
[a1, b2, c3]