Ad
  • Custom User Avatar

    First, what I can see, is memory usage, because a Stream is an Object by himself and it consumes more memory for links, metadata, etc than a single int variable in the solution below. Then filter statement use lambda which is Anonymous class instead of plain if statement. Not sure about performance, because I don't know a lot about how stream executes, but it can be easily tested. In another hand, stream can be executed parallel (check Arrays.parallelStream() or Stream.parallelStream()) what is easier to use than write parallel code by yourself.

    In conclusion, I will say that for this kind of tasks it's better to use default loops and ifs, because task is pretty simple and solution will look clean and easy to read. But when you have a lot of intermediate operations with objects streams will look much sweeter.