将几种方法组合成唯一的一种。泛型

Combining a few methods into the only one. Generics

我有一些代码可以确定与 BigInteger 相比,对不同数据类型(int、long、double)的操作的正确性。操作是获取数字的阶乘,直到结果与 BigInteger 相同。

问题是我如何更改我的代码,使其更通用、更紧凑和更简洁?我怎么能得到唯一的一种方法,而不是 4 种不同类型的方法? 此方法中的逻辑与流程几乎相同。

代码(没有比较逻辑)是:

private static HashMap<BigInteger, BigInteger> bigIntegerFactorials = new HashMap<>();
private static BigInteger bigIntegerFactorial(BigInteger number) {
    if (number.equals(BigInteger.ONE)) {
        return BigInteger.ONE;
    }
    BigInteger result = bigIntegerFactorials.get(number);
    if (result == null) {
        result = number.multiply(bigIntegerFactorial(number.subtract(BigInteger.ONE)));
        bigIntegerFactorials.put(number, result);
    }
    return result;
}

private static HashMap<Integer, Integer> intFactorials = new HashMap<>();
private static int intFactorial(int number) {
    if (number == 1) {
        return 1;
    }
    Integer result = intFactorials.get(number);
    if (result == null) {
        result = number * intFactorial(number - 1);
        intFactorials.put(number, result);
    }
    return result;
}

private static HashMap<Long, Long> longFactorials = new HashMap<>();
private static long longFactorial(long number) {
    if (number == 1) {
        return 1L;
    }
    Long result = longFactorials.get(number);
    if (result == null) {
        result = number * longFactorial(number - 1);
        longFactorials.put(number, result);
    }
    return result;
}

private static HashMap<Double, Double> doubleFactorials = new HashMap<>();
private static double doubleFactorial(double number) {
    if (number == 1) {
        return 1.;
    }
    Double result = doubleFactorials.get(number);
    if (result == null) {
        result = number * doubleFactorial(number - 1);
        doubleFactorials.put(number, result);
    }
    return result;
}

非常感谢。

如果您真的想计算阶乘,那么自 factorial only applies to integer values 以来就不需要 doubleBigDouble。既然这是真的,你可以将任何整数类型转换为 BigInteger 并使用一个方法接受任何 Number returns BigInteger.

这是一个测试class

public class Junk {
    public static void main(String[] args) {
        long val = 9;
        Junk j = new Junk();
        System.out.println(val + "! = " + j.factorial(val));
        BigInteger nine = new BigInteger("9");
        System.out.println(nine + "! = " + j.factorial(nine));
        short nine_short = 9;
        System.out.println(nine_short + "! = " + j.factorial(nine_short));
    }

    private HashMap<BigInteger, BigInteger> map = new HashMap<>();


    public BigInteger factorial(Number number){
        if(1 == number.intValue()){
            return BigInteger.ONE;
        }

        BigInteger bigInteger = new BigInteger(number.toString());
        BigInteger result = map.get(bigInteger);

        if(result == null){
            result = bigInteger.multiply(factorial(bigInteger.subtract(BigInteger.ONE)));
            map.put(bigInteger,result);
        }

        return result;
    }
}

和输出

9! = 362880
9! = 362880
9! = 362880

您可以将乘法和减量函数传递给通用方法:

private static Map<Number, Number> factorials = new HashMap<> ();

private static <T extends Number> T factorial(T n, BinaryOperator<T> multiply, UnaryOperator<T> decrement) {
  if (n.doubleValue() == 1) return n;
  T result = (T) factorials.get(n);
  if (result == null ){
    result = multiply.apply(n, factorial(decrement.apply(n), multiply, decrement));
    factorials.put(n, result);
  }
  return result;
}

然后你可以像这样改变你的原始方法:

public static int intFactorial(int number) {
  return factorial(number, (i, j) -> i * j, i -> i - 1);
}

警告:此方法似乎会使 Netbeans 崩溃,但可以使用 javac 正常编译...