Advertisement
DulcetAirman

sum(T[] array)

Jul 25th, 2018
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement