前言
JDK1.8版本的java内置四大核心函数式接口,在java.util.function,可以使用lambda表示式。
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
Comsumer 消费型接口 | T | void | 对类型为T的对象应用操作,包含方法: void accept(T t) |
Function<T, R> 函数型接口 | T | R | 对类型为T的对象应用操作,并返回结果。结果位R类型的对象。包含方法: R apply(T t) |
Predicate 断定型接口 | T | boolean | 确定类型为T的对象是否满足某约束,并返回boolean值。包含方法: boolean test(T t) |
Supplier 供给型接口 | 无 | T | 返回类型为T的对象,包含方法: T get() |
2、Consumer
消费类型,即只有支出没有收入(有入参,没有返回值)
- 源码
@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); };
}
}
- 代码示例 1
public static void main(String[] args){
List<String> list = Lists.newArrayList();
Consumer<String> consumer = s-> {
if(StringUtils.isNotEmpty(s)){
list.add(s);
}
}
consumer.aceept("test");
log.info("====={}",list);
}
// 输出结果:=====[test]
- 代码示例 2
public static void main(String[] args) {
List<String> list = Lists.newArrayList();
Consumer<String> consumer = s-> {
if(StringUtils.isNotEmpty(s)){
list.add(s);
}
};
consumer = consumer.andThen(s->{
if("test".equals(s)){
list.add("test521");
}
});
consumer.accept("test");
log.info("====={}",list);
}
// 输出结果:=====[test, test521]
3、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;
}
}
- 代码示例 1
public static void main(String[] args){
Function<Integer, Integer> function = x-> x+1;
System.out.println(function.apply(10));
}
// 输出结果:11
- 代码示例 2
public static void main(String[] args){
Function<Integer, Integer> function = x-> x+1;
Function<Integer, Integer> composeF = x-> x*2;
System.out.println(function.compose(composeF).apply(10));
}
// 输出结果:21
// 执行逻辑:先执行 composeF,再执行 function
- 代码示例 3
public static void main(String[] args){
Function<Integer, Integer> function = x-> x+1;
Function<Integer, Integer> andThenF = x-> x*2;
System.out.println(function.andThen(andThenF).apply(10));
}
// 输出结果:22
// 执行逻辑:先执行 function,再执行 andThenF
- 代码示例 4
public static void main(String[] args){
System.out.println(Function.identity().apply("我只会把参数当做返回值"));
}
// 输出结果:我只会把参数当做返回值
4、Predicate
断定类型,既有参数,也有返回值,但是返回值只能是boolean
- 源码
@FunctionalInterface
public interface Predicate<T> {
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);
}
@SuppressWarnings("unchecked")
static <T> Predicate<T> not(Predicate<? super T> target) {
Objects.requireNonNull(target);
return (Predicate<T>)target.negate();
}
}
- 代码示例 1
public static void main(String[] args){
Predicate<Integer> predicate = x->x%2==0;
System.out.println(predicate.test(99));
// 输出结果:false
System.out.println(predicate.test(100));
// 输出结果:true
}
代码示例 2
public static void main(String[] args){
Predicate<Integer> predicate = x->x%2==0;
System.out.println(predicate.negate().test(9));
// 输出结果:true
System.out.println(predicate.negate().test(10));
// 输出结果:false
System.out.println(Stream.of(2, 3).filter(predicate.negate()).count());
// 输出结果:1
}
- 代码示例 3
public static void main(String[] args){
Predicate<Integer> predicate = x->x%2==0;
Predicate<Integer> andPredicate = x-> x > 10;
System.out.println(predicate.and(andPredicate).test(8));
// 输出结果:false
System.out.println(predicate.and(andPredicate).test(9));
// 输出结果:false
System.out.println(predicate.and(andPredicate).test(11));
// 输出结果:false
System.out.println(predicate.and(andPredicate).test(12));
// 输出结果:true
}
- 代码示例 4
public static void main(String[] args){
Predicate<Integer> equalPredicate = Predicate.isEqual(5);
System.out.println(equalPredicate.test(5));
// 输出结果:true
System.out.println(equalPredicate.test(6));
// 输出结果:false
}
4、Supplier
供给类型,只有返回值,没有参数
- 源码
@FunctionalInterface
public interface Supplier<T> {
T get();
}
- 代码示例
public static void main(String[] args){
Supplier<String> supplier = ()->{
return "我是测试数据";
};
System.out.println(supplier.get())
}