# Sum Of Integer Array : How To Do Functional Programming In Java

We can use functional Programming in Java to calculate sum of an int array.

You can watch my live coding video to see how it is done.

```int sum = 0;
for(int intValue : ints) sum += intValue;```

Functional Programming Way 1 :-

`Arrays.stream(ints).sum();`

Functional Programming Way 2 :-

`Arrays.stream(ints).reduce(0, Integer::sum);`

Functional Programming Way 3:-

```final AtomicInteger sumInt = new AtomicInteger(0);
Arrays.stream(ints).forEach( intValue -> sumInt.set( intValue + sumInt.get()));```

Source Code:-

```import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

public class SumOfIntArray{
public static void main(String ...args){
final int[] ints = {1, 2, 3, 4, 5, 6};

int sum = 0;
for(int intValue : ints) sum += intValue;

sum = 0;

sum = Arrays.stream(ints).sum();
System.out.println("Functional Stream 'sum' function Sum " + sum);

sum = 0;

sum = Arrays.stream(ints).reduce(0, Integer::sum);
System.out.println("Functional Stream 'reduce' function Sum " + sum);

final AtomicInteger sumInt = new AtomicInteger(0);
Arrays.stream(ints).forEach( intValue -> sumInt.set( intValue + sumInt.get()));
System.out.println("Functional Stream should not be done this way " + sumInt.get());

}
}```