java - Is there possibility of sum of ArrayList without looping -
is there possibility of sum of arraylist
without looping?
php provides sum(array)
give sum of array.
the php code like
$a = array(2, 4, 6, 8); echo "sum(a) = " . array_sum($a) . "\n";
i wanted same in java:
list tt = new arraylist(); tt.add(1); tt.add(2); tt.add(3);
once java-8 out (march 2014) you'll able use streams:
if have list<integer>
int sum = list.stream().maptoint(integer::intvalue).sum();
if it's int[]
int sum = intstream.of(a).sum();
Comments
Post a Comment