java – Java 8 Stream and array operations

You can turn an array into a stream using

int[] ns = new int[] {1,2,3,4,5};;

Once the flow is obtained, you can use one of the methods described in the documentation how sum() or anything else. You can map or filter as in Python by calling the relevant flow methods with a Lambda function: -> n * 2); -> n % 4 == 0);

Once you’re done editing your stream, you call toArray() to convert it back to an array for use elsewhere:

int[] ns = new int[] {1,2,3,4,5};
int[] ms = -> n * 2).filter(n -> n % 4 == 0).toArray();

Leave a comment