Is there possibility of sum of ArrayList
without looping?
PHP provides sum(array)
which will give the sum of array.
The PHP code is
This link shows three different ways how to sum in java, there is one option that is not in previous answers using Apache Commons Math..
Example:
public static void main(String args []){
List NUMBERS_FOR_SUM = new ArrayList(){
{
add(5D);
add(3.2D);
add(7D);
}
};
double[] arrayToSume = ArrayUtils.toPrimitive(NUMBERS_FOR_SUM
.toArray(new Double[NUMBERS_FOR_SUM.size()]));
System.out.println(StatUtils.sum(arrayToSume));
}
See StatUtils api