Java 8中是否有预定义函数可以执行以下操作:
static <T, R> Function<T, R> constant(R val) {
return (T t) -> {
return val;
};
}
Run Code Online (Sandbox Code Playgroud)
在我尝试将整数解析为罗马数字时,回答人们对我为什么需要此函数的查询是真正的用法:
// returns the stream of roman numeral symbol based
// on the digit (n) and the exponent (of 10)
private static Stream<Symbol> parseDigit(int n, int exp) {
if (n < 1) return Stream.empty();
Symbol base = Symbol.base(exp);
if (n < 4) {
return IntStream.range(0, n).mapToObj(i -> base);
} else if (n == 4) {
return Stream.of(base, Symbol.fifth(exp));
} else if …Run Code Online (Sandbox Code Playgroud)