1

I have gone through a few web resources and links, however, I am unable to find the sum of elements of an Integer ArrayList without looping. I am looking for a function which will allow me to do this in a single line.

I was able to find the same for a normal array as follows

import java.util.ArrayList;
import java.util.stream.IntStream;

public class sumArray
{
    public static void main(String[] args)
    {
        int[] a = {10,20,30,40,50};
        int sum = IntStream.of(a).sum();
        System.out.println("The sum is " + sum);
    }
}

I can write a user-defined function as follows

import java.util.ArrayList;

public class sumAL
{
    public static void main(String[] args)
    {
        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(1);
        al.add(3);
        al.add(5);

        System.out.println(sum(al));
    }

    static int sum(ArrayList<Integer> al)
    {
        int value = 0;
        for(int i : al)
        {
            value += i;
        }
        return value;
    }
}

However, I'm looking for something in-built. Please advise.

EDIT : I have tried the following but I get build errors

import java.util.ArrayList;

public class sumAL
{
    public static void main(String[] args)
    {
        System.out.println(getVersion());

        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(1);
        al.add(3);
        al.add(5);
        System.out.println(al.stream().mapToInt(Integer::intValue).sum());
    }

    static double getVersion () {
        String version = System.getProperty("java.version");
        int pos = version.indexOf('.');
        pos = version.indexOf('.', pos+1);
        return Double.parseDouble (version.substring (0, pos));
    }
}

Errors

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Integer cannot be resolved to a variable
    Syntax error on tokens, delete these tokens
saltandwater
  • 791
  • 2
  • 9
  • 25
  • one way or another some looping will be used, you can use a divide-and-conquer recursive (or iterative) approach since sums satisfy the partial sums property, i.e a **sum is equal to the sum of partial sums** – Nikos M. Aug 19 '15 at 16:07
  • 6
    possible duplicate of [Is there possibility of sum of ArrayList without looping](http://stackoverflow.com/questions/5963847/is-there-possibility-of-sum-of-arraylist-without-looping) – Swapnil Aug 19 '15 at 16:07
  • @saltandwater What errors do you get? – kajacx Aug 19 '15 at 16:29
  • @kajacx, please see edit above for errors – saltandwater Aug 19 '15 at 16:56
  • Are you sure you are using java 8? Run `java -version` from command line or check java instalation folder for version. Streams (you are using those) are only avaliable in java 8. – kajacx Aug 19 '15 at 17:13
  • java -version gives me 1.8. I also tested using getVersion() code above and it gives me 1.8 – saltandwater Aug 19 '15 at 17:17

2 Answers2

1

You can easily map a Stream<Integer> to an IntStream and then calculate the sum :

a1.stream().mapToInt(Integer::intValue).sum();
Eran
  • 387,369
  • 54
  • 702
  • 768
1
al.stream().reduce(0, (x,y) -> x+y)
FoxyBOA
  • 5,788
  • 8
  • 48
  • 82
  • 2
    While this code may answer the question, providing additional context regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. – spongebob Aug 19 '15 at 16:55