Sum elements of arraylist

This example will show how to sum elements of arraylist in groovy. The first snippet will sum numbers while the second will set an intial value before suming the elements in the list. A related example of summing numbers in java.

Sum numbers in list

@Test
void sum_numbers_in_list() {
    
    assert 10, [1, 2, 3, 4].sum();
}

Sum with inital value

@Test
void sum_with_initial_value() {
    
    assert 110, [1, 2, 3, 4].sum(100);
}