使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口。函数式接口主要分为:
- Supplier供给型函数:Supplier的表现形式为不接受参数、只返回数据。
- Consumer消费型函数:Consumer接收一个参数,没有返回值。
- Runnable无参无返回型函数:Runnable的表现形式为即没有参数也没有返回值。
- Function有参有返回型函数:Function函数的表现形式为接收一个参数,并返回一个值。
1. Supplier
java.util.function.Supplier 接口仅包含一个无参的方法: T get() 。用来获取一个泛型参数指定类型的对象数据。由于这是一个函数式接口,这也就意味着对应的Lambda表达式需要“对外提供”一个符合泛型类型的对象数据。
Supplier
案例1:
import org.junit.Test;
import java.util.function.Supplier;
public class Demo1 {
public static int getMax(Supplier<Integer> sup) {
return sup.get();
}
@Test
public void c_求数组元素最大值() {
int[] arr = {1, 5, 6, 4, 8, 7, 52, 2, 111};
int max1 = getMax(() -> {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
if (temp > max) {
max = temp;
}
}
return max;
});
System.out.println("max1 = " + max1);
}
}
案例2:
import org.junit.Test;
import java.util.function.Supplier;
public class Test01 {
private static String getString(Supplier<String> function) {
return function.get();
}
@Test
public void c_() {
String string = getString(() -> "hello" + "world");
System.out.println("string = " + string);
}
}
2. Consumer
Consumer
void accept(T t);
java.util.function.Consumer 接口则正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据,
其数据类型由泛型决定。
抽象方法:accept
Consumer 接口中包含抽象方法 void accept(T t) ,意为消费一个指定泛型的数据。
案例1:
import java.util.function.Consumer;
/**
* 下面的字符串数组当中存有多条信息,请按照格式“ 姓名:XX。性别:XX。 ”
* 的格式将信息打印出来。要求将打印姓名的动作作为第一个 Consumer 接口的Lambda实例,将打印性别的动作作为第二个 Consumer 接口的Lambda实例,将两个 Consumer 接口按照顺序“拼接”到一起。
*/
public class Demo01 {
public static void main(String[] args) {
String[] array = {"迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男"};
printInfo(s -> System.out.print("姓名:" + s.split(",")[0]),
s -> System.out.println("。性别:" + s.split(",")[1] + "。"), array);
}
private static void printInfo(Consumer<String> one, Consumer<String> two, String[] array) {
for (String info : array) {
one.andThen(two).accept(info); // 姓名:迪丽热巴。性别:女。
}
}
}
案例2:
import java.util.function.Consumer;
public class Test01 {
private static void consumerString(Consumer<String> one, Consumer<String> two) {
one.andThen(two).accept("Hello");
}
public static void main(String[] args) {
consumerString(
s -> System.out.println(s.toUpperCase()),
s -> System.out.println(s.toLowerCase())
);
}
}
3. Predicate
有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用 java.util.function.Predicate 接口。
Predicate
案例1:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class Demo01 {
/**
* 数组当中有多条“姓名+性别”的信息如下,请通过 Predicate 接口的拼装将符合要求的字符串筛选到集合
* ArrayList 中,需要同时满足两个条件:
* 1. 必须为女生;
* 2. 姓名为4个字。
* @param args
*/
public static void main(String[] args) {
String[] array = {"迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女"};
List<String> result = filter(array,
s -> "女".equals(s.split(",")[1]),
s -> s.split(",")[0].length() == 4
);
System.out.println(result);
}
private static List<String> filter(String[] array, Predicate<String> one, Predicate<String> two) {
List<String> list = new ArrayList<>();
for (String info : array) {
if (one.and(two).test(info)) {
list.add(info);
}
}
return list;
}
}
案例2:
import org.junit.Test;
import java.util.function.Predicate;
public class Test01 {
/**
* “与”、“或”已经了解了,剩下的“非”(取反)也会简单。
* 对结果boolean值进行“!”取反而已
*/
@Test
public void c_negate() {
method3(s -> s.length()<5);
}
private static void method3(Predicate<String> predicate) {
boolean veryLong = predicate.negate().test("HelloWorld");
System.out.println(
"字符串很长吗:" + veryLong);
}
private static void method2(Predicate<String> one, Predicate<String> two) {
boolean isValid = one.or(two).test("Helloworld");
System.out.println("字符串符合要求吗:" + isValid);
}
@Test
public void c_or() {
method2(s -> s.contains("H"),
s -> s.contains("W")
);
}
private static void method(Predicate<String> predicate) {
boolean veryLong = predicate.test("HelloWorld");
System.out.println("字符串很长吗:" + veryLong);
}
/**
* 条件判断的标准是传入的Lambda表达式逻辑,只要字符串长度大于5则认为很长。
*/
@Test
public void c_2() {
method(s -> s.length() > 5);
}
/**
* and 方法
* 如果要判断一个字符串既要包含大写“H”,又要包含大写“W”
*/
@Test
public void c_and() {
method(s -> s.contains("H"),
s -> s.contains("W")
);
}
private static void method(Predicate<String> one, Predicate<String> two) {
boolean isValid = one.and(two).test("Helloworld");
System.out.println("字符串符合要求吗:" + isValid);
}
}
4. Function
java.util.function.Function
抽象方法:apply
Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。
Function
R apply(T t);
案例1:
import java.util.function.Function;
/**
* 请使用 Function 进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
* String str = “赵丽颖,20”;
* 1. 将字符串截取数字年龄部分,得到字符串;
* 2. 将上一步的字符串转换成为int类型的数字;
* 3. 将上一步的int数字累加100,得到结果int数字。
*/
public class Demo01 {
public static void main(String[] args) {
String str = "赵丽颖,20";
int age = getAgeNum(str, s -> s.split(",")[1],
s -> Integer.parseInt(s), n -> n += 100
);
System.out.println(age);
}
private static int getAgeNum(String str, Function<String, String> one,
Function<String, Integer> two,
Function<Integer, Integer> three) {
return one.andThen(two).andThen(three).apply(str);
}
}
案例2:
import org.junit.Test;
import java.util.function.Function;
public class Test01 {
private static void method(Function<String, Integer> function) {
int num = function.apply("10");
System.out.println(num + 20);
}
/**
* apply
*/
@Test
public void c_apply() {
method(s -> Integer.parseInt(s));
}
/**
* Function 接口中有一个默认的 andThen 方法,用来进行组合操作
* 该方法同样用于“先做什么,再做什么”的场景,和 Consumer 中的 andThen 差不多:
* 第一个操作是将字符串解析成为int数字,第二个操作是乘以10。两个操作通过 andThen 按照前后顺序组合到了一起。
* 请注意,Function的前置条件泛型和后置条件泛型可以相同。
*/
@Test
public void c_andThen() {
method2(s -> Integer.parseInt(s) + 10, integer -> integer *= 10);
}
private static void method2(Function<String, Integer> one, Function<Integer, Integer> two) {
int num = one.andThen(two).apply("10");
System.out.println(num + 20);
}
}