一、JDK1.8常用函数式接口
Supplier接口 - 用来获取一个泛型参数指定类型的对 象数据
Supplier接口
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Consumer接口 - 正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据, 其数据类型由泛型决定。
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
Predicate接口 - 对某种类型的数据进行判断,从而得到一个boolean值结果。 ```java @FunctionalInterface public interface Predicate
{ boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> negate() {
return (t) -> !test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
- **Function接口 - 来根据一个类型的数据得到另一个类型的数据,前者称为前置条件, 后者称为后置条件。**
```java
@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;
}
}
二、函数式接口应用
- Supplier get方法应用
```java
public static String getString(Supplier
supplier){ return supplier.get(); } public static void main(String[] args) { String str = getString(() -> {
}); System.out.println(str); }return "hello";
— 结果 hello
- **Consumer accept接口**
- **Consumer andThen方法相当于调用两次accept**
```java
public static void accept(String str, Consumer<String> consumer){
consumer.accept(str);
}
public static void main(String[] args) {
accept("hello world", (str) -> {
String[] strArr =str.split(" ");
System.out.println(strArr[0]);
});
}
--- 输出结果 hello
public static void acceptAndThen(String str, Consumer<String> consumer1, Consumer<String> consumer2){
consumer1.andThen(consumer2).accept(str);
}
public static void main(String[] args) {
acceptAndThen("hello world",
(String str1) -> {
String a = str1 + " a";
System.out.println(a);
},
(String str2) -> {
String b = str2 + " b";
System.out.println(b);
});
}
-- 输出结果
hello world a
hello world b
- Predicate test方法
- Predicate and方法,两个test的逻辑与 &&
- Predicate or方法,两个test的或 ||
- Predicate negate方法,对test方法进行去反
```java
public class Demo15PredicateTest {
private static void method(Predicatepredicate) {
}boolean veryLong = predicate.test("HelloWorld");
System.out.println("字符串很长吗:" + veryLong);
public static void main(String[] args) {
} }method(s ‐> s.length() > 5);
private static void method(Predicate
System.out.println(“字符串符合要求吗:” + isValid);
}
public static void main(String[] args) {
method(s ‐> s.contains(“H”), s ‐> s.contains(“W”));
}
- **Function apply接口**
Function 接口中主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。
```java
public class Demo11FunctionApply {
private static void method(Function<String, Integer> function) {
int num = function.apply("10");
System.out.println(num + 20);
}
public static void main(String[] args) {
method(s ‐> Integer.parseInt(s));
}
}