Java8中是否有标准化的通用功能接口?

时间:2016-07-20 08:04:18

标签: java functional-interface

我想知道是否有人在哪里定义了一组通用功能接口以简化和标准化lambdas的使用。 我来自C#,核心库完成了这项工作。

我读到了" BiFunction"并发现它很笨拙。特别是扩展到TriFunction等时。

我想出了以下内容:

public interface FI {
@FunctionalInterface
public interface Action<T> {
    void invoke(T arg1);
}
@FunctionalInterface
public interface Function0<TResult> {
    TResult invoke();
}
@FunctionalInterface
public interface Function1<Targ1, TResult> {
    TResult invoke(Targ1 arg1);
}
@FunctionalInterface
public interface Function2<Targ1, Targ2, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2);
}
@FunctionalInterface
public interface Function3<Targ1, Targ2, Targ3, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3);
}
@FunctionalInterface
public interface Function4<Targ1, Targ2, Targ3, Targ4, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4);
}
@FunctionalInterface
public interface Function5<Targ1, Targ2, Targ3, Targ4, Targ5, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5);
}
@FunctionalInterface
public interface Function6<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6);
}
@FunctionalInterface
public interface Function7<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7);
}
@FunctionalInterface
public interface Function8<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8);
}
@FunctionalInterface
public interface Function9<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8, Targ9 arg9);
}
@FunctionalInterface
public interface Function10<Targ1, Targ2, Targ3, Targ4, Targ5, Targ6, Targ7, Targ8, Targ9, Targ10, TResult> {
    TResult invoke(Targ1 arg1, Targ2 arg2, Targ3 arg3, Targ4 arg4, Targ5 arg5, Targ6 arg6, Targ7 arg7, Targ8 atg8, Targ9 arg9, Targ10 arg10);
}

}

3 个答案:

答案 0 :(得分:2)

javaslang [1]提供Function0 - Function8甚至CheckedFunction0 - CheckedFunction8。它们甚至提供了“memoization”和“currying”等附加功能。

http://javaslang.io

答案 1 :(得分:0)

在Java 8中官方只存在4种类型的函数。

  Consumer: Void function
  Function: receive argument return argument
  BiFunction: Receive 2 arguments return argument
  Predicate: receive argument return boolean

在这里你可以看到一些关于它的例子。

https://github.com/politrons/reactive/blob/master/src/test/java/stream/Functions.java

答案 2 :(得分:0)

您可以在java.util.function包中找到最接近的类似物:

git clone https://github.com/rahulkatariya/osx-terminal.app-colors-solarized.git

mv osx-terminal.app-colors-solarized/Solarized\ Dark.terminal osx-terminal.app-colors-solarized/Solarized\ Dark.plist

solarizedDark=`/usr/libexec/PlistBuddy -c "Print" osx-terminal.app-colors-solarized/Solarized\ Dark.plist 2>&1`

/usr/libexec/PlistBuddy -c "Add :Window\ Settings:Solarized\ Dark dict $solarizedDark" ~/Library/Preferences/com.apple.Terminal.plist

等。您可以定义自己的,也可以在Google Guava或其他库中找到一些。