javaintegerjava-streamatomic

Java AtomincInteger vs one element array for streams


int[] arr = new int[]{0};
l.stream().forEach(x -> {if (x > 10 && x < 15) { arr[0] += 1;}});

l is List<Integer>. Here I use one element arr array to store value that is changed inside the stream. An alternative solution is to use an instance of AtomicInteger class. But I don't understand what is the difference between these two approaches in terms of memory usage, time complexity, safety...

Please note: I am not trying to use AtomicInteger (or array) in this particular piece of code. This code is used only as an example. Thanks!


Solution

  • You should always use AtomicInteger:

    But why?

    Lambdas are not transparent in the following 3 things:

    These are all very bad things when the lambda runs 'in context', but they are all very good things when the lambda doesn't run in context.

    Here is an example:

    new TreeSet<String>((a, b) -> a - b);
    

    Here I have created a TreeSet (which is a set that keeps its elements sorted automatically). To make one, you pass in code that determines for any 2 elements which one is 'the higher one', and TreeSet takes care of everything else. That TreeSet can survive your method (just store it in a field or pass it to a method that ends up storing it in a field) and could even escape your thread (have another thread read that field). That means when that code (a - b in this code) is invoked, we could be 5 days from the creation of that TreeSet, in another thread, with the code that 'surrounds' your new TreeSet statement having loooong gone.

    In this scenario, all those transparencies make no sense at all:

    What does it mean to break back to a loop that has long since completed and the system doesn't even know what it is about anymore?

    That catch block uses context that is long gone, such as local vars or the parameters. It can't survive, so if your a - b were to throw something that is checked, the fact that you've wrapped your new TreeSet<> in a try/catch block is meaningless.

    What does it mean to access a variable that no longer exists? For that matter, if it still does exist but the lambda runs in a separate thread, do we now start making local vars volatile and declare them on heap instead of stack just in case?

    Of course, if your lambda runs within context, as in, you pass the lambda to some method and that method 'uses it or loses it': Runs your lambda a certain amount of times and then forgets all about it, then those lacking transparencies are really annoying.

    It's annoying that you can't do this:

    public List<String> toLines(List<Path> files) throws IOException {
      var allLines = files.stream()
        .filter(x -> x.toString().endsWith(".txt"))
        .flatMap(x -> Files.readAllLines().stream())
        .toList();
    }  
    

    The only reason the above code fails is that Files.readAllLines() throws IOException. We declared that we throws this onwards but that won't work. You have to kludge up this code, make it bad, by trying to somehow transit that exception out of the lambda or otherwise work around it (the right answer is NOT the use the stream API at all here, write it with a normal for loop!).

    Whilst trying to dance around checked exceptions in lambdas is generally just not worth it, you CAN work around the problem of wanting to share a variable with outer context:

    int sum = 0;
    listOfInts.forEach(x -> sum += x);
    

    The above doesn't work - sum is from the outer scope and thus must be effectively final, and it isn't. There's no particular reason it can't work, but java won't let you. The right answer here is to use int sum = listOfInts.mapToInt(Integer::intValue).sum(); instead, but you can't always find a terminal op that just does what you want. Sometimes you need to kludge around it.

    That's where new int[1] and AtomicInteger comes in. These are references - and the reference is final, so you CAN use them in the lambda. But the reference points at an object and you can change it at will, hence, you can use this 'trick' to 'share' a variable:

    AtomicInteger sum = new AtomicInteger();
    listOfInts.forEach(x -> sum.add(x));
    

    That DOES work.