在实用程序类中对常见的 lambda 表达式进行分组以避免代码重复是否被认为是好的做法?
最好的方法是什么?现在,我有一个带有一些 public static final Functions 成员的 MathUtils 类:
public class MathUtils{
public static final Function<Long, Long> triangle = n -> n * (n + 1) / 2,
pentagonal = n -> n * (3 * n - 1) / 2,
hexagonal = n -> n * (2 * n - 1);
}
最佳答案
你也可以这样做
public class MathUtils
{
public static long triangle(long n)
{
return n * (n + 1) / 2;
}
像这样使用它
MathUtils::triangle
取决于您的品味和用例。
关于java - 公共(public)静态最终 Lambda?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483662/