Since Number
does not provide any subtract
method, you can't do it simply. The alternatives I can think of are:
- use the provided
doubleValue()
method, and return adouble
, but you could lose precision if the lists contain BigDecimal for example. - have one method per available
Number
subclass, but it could create a lot of duplicated code... - restrict the type to whatever makes sense (say double if precision does not matter too much or BigDecimal if it does), which is a subcase of (2).
Option 2 could be implemented like by replacing difference.add(currMinuend-subtrahend.get(i));
by difference.add(subtract(currMinuend, subtrahend.get(i)));
Then it's only a matter of writing 10 subtract
methods:
private static int subtract(int a, int b) { return a - b; }
private static double subtract(double a, double b) { return a - b; }
private static BigDecimal subtract(BigDecimal a, BigDecimal b) { return a.subtract(b); }
etc.