java函数编程(lambda)

什么是lambda

java的lambda是java8的新特性(现在都要出java12了,才来聊这个东东,显得有点落后。不得不说,如果不是有一次看谁的代码,看到这行代码:“System.out::println”,我还不知道java已经有了这个东西…),简单的说就是把自定义方法当成参数传递。我最早接触函数编程的语言应该是js,当时见到可以在传参中写那么大一段代码时:“WC,居然还可以这么干”。现在没想到java也可以这么干了。相对于弱类型语言的js来说,java的这个新功能看起来就没那么简单了(虽然它有根据上下文推测变量类型的功能)。

废话不多说,进入正题吧。

关于java的lambda

java的lambda就是一个一个的接口类,java规定lambda接口中有且仅有一个抽象接口,多了不行,没有也不行。在自定义lambda接口时,最好给类加上@FunctionalInterface注解,@FunctionalInterface注解的作用是告诉编译器检查当前接口是不是只包含一个抽象方法。具体就是说,注解在Inteface上,且interface里只能有一个抽象方法,可以有default方法。因为从语义上来讲,一个函数式接口需要通过一个逻辑上的方法表达一个单一函数。那理解这个单一就很重要了,单一不是说限制你一个interface里只有一个抽象方法,单是多个方法的其他方法需要是继承自Object的public方法,或者你要想绕过,就自己实现default。函数式接口自己本身一定是只有一个抽象方法。同时,如果是Object类的public方法,也是不允许的。官方的说明翻译如下:

如果一个接口I,I有一组抽象方法集合M,且这些方法都不是Object类的public签名方法,那么如果存在一个M中的方法m,满足:

m的签名是所有M中方法签名的子签名。

m对于M中的每个方法都是返回类型可替换的。 此时,接口I是一个函数式接口。

简单的lambda实现

自定义一个函数接口(也可以用java自带的Function接口):

@FunctionalInterface
public interface LambdaTest<T, R> {
	// 这是带有返回值的抽象方法
	R test(T t);
}

测试代码:

public class MyLambdaTest {
	public static void main(String[] args){
		lambdaTest(x -> x + 1);
	}
	private static void lambdaTest(LambdaTest<Integer, Integer> fun) {
		Integer a = 2;
		System.out.println(fun.test(a));
	}
}

输出:
3

可以看到,lambdaTest方法的入参是一个接口,在调用的时候,直接将方法内容传进去,也就是代码中的

x -> x + 1

表达式使用“->”连接,左边为入参,右边为执行代码。参数可以表明类型,可以省略。当没有指明参数类型时,java会根据上下文自主来判断入参的类型。如果带上参数类型的话,就需要用括号包起来,想这样“(Integer x)”。右边为自定义执行代码(相当于方法体),可以是一句,可以是几句,当有几句代码时,需要用“{}”包围,当使用了“{}”并且方法需要有返回值时,必须要写return:

public class MyLambdaTest {
	public static void main(String[] args){
		lambdaTest((Integer x) -> {
			System.out.println(x);
			return x + 1;
		});
	}
	private static void lambdaTest(LambdaTest<Integer, Integer> fun) {
		Integer a = 2;
		System.out.println(fun.test(a));
	}
}

输出:
2
3

java自带函数接口

在jdk中,实现了很多的函数接口,它们都定义在java.util.function包下,大致可以分为这几类:Function、Predicate、Supplier、Consumer和*Operator(没有Operator接口,只有类似BinaryOperator这样的接口),我们来看看这些接口:

Function

@FunctionalInterface
public interface Function<T, R> {
	R apply(T t);
	...
}

函数意为将参数T传递给一个函数,返回R。即 R = F u n c t i o n ( T ) R=Function(T) R=Function(T)

其默认实现了3个default方法,分别是compose、andThen和identity,对应的函数表达为:compose对应 V = F u n c t i o n ( P a r a m F u n c t i o n ( T ) ) V=Function(ParamFunction(T)) V=Function(ParamFunction(T)),体现嵌套关系;andThen对应 V = P a r a m F u n c t i o n ( F u n c t i o n ( T ) ) V=ParamFunction(Function(T)) V=ParamFunction(Function(T)),转换了嵌套的顺序;还有identity对应了一个传递自身的函数调用对应 F u n c t i o n ( T ) = T Function(T)=T Function(T)=T。从这里看出来,compose和andThen对于两个函数f和g来说,f.compose(g)等价于g.andThen(f)。

Function接口相关的接口包括:

BiFunction :R apply(T t, U u);接受两个参数,返回一个值,代表一个二元函数;

DoubleFunction :R apply(double value);只处理double类型的一元函数;

IntFunction :R apply(int value);只处理int参数的一元函数;

LongFunction :R apply(long value);只处理long参数的一元函数;

ToDoubleFunction:double applyAsDouble(T value);返回double的一元函数;

ToDoubleBiFunction:double applyAsDouble(T t, U u);返回double的二元函数;

ToIntFunction:int applyAsInt(T value);返回int的一元函数;

ToIntBiFunction:int applyAsInt(T t, U u);返回int的二元函数;

ToLongFunction:long applyAsLong(T value);返回long的一元函数;

ToLongBiFunction:long applyAsLong(T t, U u);返回long的二元函数;

DoubleToIntFunction:int applyAsInt(double value);接受double返回int的一元函数;

DoubleToLongFunction:long applyAsLong(double value);接受double返回long的一元函数;

IntToDoubleFunction:double applyAsDouble(int value);接受int返回double的一元函数;

IntToLongFunction:long applyAsLong(int value);接受int返回long的一元函数;

LongToDoubleFunction:double applyAsDouble(long value);接受long返回double的一元函数;

LongToIntFunction:int applyAsInt(long value);接受long返回int的一元函数;

Operator

Operator其实就是一个返回值类型和参数类型一直的Function,其包括两个接口:UnaryOperator和BinaryOperator,UnaryOperator是一元函数:

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

BinaryOperator则是二元函数:

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
	public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }
    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }

其他的Operator接口:(不解释了)

LongUnaryOperator:long applyAsLong(long operand);

IntUnaryOperator:int applyAsInt(int operand);

DoubleUnaryOperator:double applyAsDouble(double operand);

DoubleBinaryOperator:double applyAsDouble(double left, double right);

IntBinaryOperator:int applyAsInt(int left, int right);

LongBinaryOperator:long applyAsLong(long left, long right);

Predicate

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);
    }

	@SuppressWarnings("unchecked")
    static <T> Predicate<T> not(Predicate<? super T> target) {
        Objects.requireNonNull(target);
        return (Predicate<T>)target.negate();
    }
    ...
}

它除了抽象接口,还默认提供了and、or、not和negate逻辑。
其他Predicate接口:

BiPredicate:boolean test(T t, U u);接受两个参数的二元谓词

DoublePredicate:boolean test(double value);入参为double的谓词函数

IntPredicate:boolean test(int value);入参为int的谓词函数

LongPredicate:boolean test(long value);入参为long的谓词函数

Consumer

这是一个不需要返回值的函数接口:

@FunctionalInterface
public interface Consumer<T> {
	void accept(T t);
}

其他Consumer接口:

BiConsumer:void accept(T t, U u);接受两个参数

DoubleConsumer:void accept(double value);接受一个double参数

IntConsumer:void accept(int value);接受一个int参数

LongConsumer:void accept(long value);接受一个long参数

ObjDoubleConsumer:void accept(T t, double value);接受一个泛型参数一个double参数

ObjIntConsumer:void accept(T t, int value);接受一个泛型参数一个int参数

ObjLongConsumer:void accept(T t, long value);接受一个泛型参数一个long参数

Supplier

Supplier是一个不需要参数且有返回值的函数接口,它就像一个创造者:

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

其他Supplier接口:

BooleanSupplier:boolean getAsBoolean();返回boolean

DoubleSupplier:double getAsDouble();返回double

IntSupplier:int getAsInt();返回int

LongSupplier:long getAsLong();返回long

总结

整个函数式接口的大概总结如下:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值