Advertisement
DulcetAirman

sum(T[] array)

Jul 25th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. package ch.claude_martin.foo;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.BigInteger;
  5. import java.util.Arrays;
  6.  
  7. public class SomeClass {
  8.     public static void main(String[] args) {
  9.         System.out.println(sum(new Number[] {0,42,-Math.PI, BigInteger.valueOf(12345)}));
  10.     }
  11.  
  12.     public static <T extends Number> BigDecimal sum(T[] array) {
  13.         return Arrays.stream(array).map(n -> new BigDecimal(n.toString())).reduce(BigDecimal.ZERO, BigDecimal::add);
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement