SHARE
TWEET

sum(T[] array)

DulcetAirman Jul 25th, 2018 68 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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top