函数式接口:一个接口中只包含一个方法,例如Runnable和Callable接口,分别只有一个run()和call()。
Consumer :消费型接口
只有输入,没有返回
package com.ys.function;
import java.util.function.Consumer;
public class Demo3 {
public static void main(String[] args) {
// Consumer<String> consumer = new Consumer<String>() {
// @Override
// public void accept(String s) {
// System.out.println(s);
// }
// };
Consumer<String> consumer = (str)->{
System.out.println("zhangsan");
};
consumer.accept("dadas");
}
}
Function:函数型接口
查看Function接口的源码:
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.util.function;
import java.util.Objects;
/**
* Represents a function that accepts one argument and produces a result.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(Object)}.
*
* @param <T> the type of the input to the function
* @param <R> the type of the result of the function
*
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
只有一个方法:apply。Function接口有两个参数,传入T和R,通过apply方法传入T,返回R。
测试代码:
package com.ys.function;
import java.util.function.Function;
public class Demo1 {
public static void main(String[] args) {
Function<String, String> function = new Function<String,String>() {
@Override
public String apply(String o) {
return o;
}
};
System.out.println(function.apply("张三"));
}
}
当然函数式接口可以采用lambda表达式的方式去编写。
package com.ys.function;
import java.util.function.Function;
public class Demo1 {
public static void main(String[] args) {
Function<String, String> function = (string)->{
return string;
};
System.out.println(function.apply("张三"));
}
}
Supplier :供给型接口
没有参数,但是有返回值
package com.ys.function;
import java.util.function.Supplier;
public class Demo4 {
public static void main(String[] args) {
// Supplier<String> supplier = new Supplier<String>() {
// @Override
// public String get() {
// return "zhangsan";
// }
// };
Supplier<String> supplier =()->{
return "张三";
};
System.out.println(supplier.get());
}
}
Predicate 断定型接口,有一个参数,返回值只能是布尔值。
可以用来判断字符串是否为空:
改用lambda表达式的方式改写:
package com.ys.function;
import java.util.function.Predicate;
public class Demo2 {
public static void main(String[] args) {
// Predicate<String> predicate = new Predicate<String>() {
// @Override
// public boolean test(String str) {
// return false;
// }
// };
Predicate<String> predicate = (str)->{ return str.isEmpty(); };
System.out.println(predicate.test("zhangsan"));
}
}
总结:那么函数式接口的目的是什么?
主要是简化编程模型。在框架中使用比较多。
函数式接口使用的注解是 @FunctionalInterface