You can turn an array into a stream using Arrays.stream()
:
int[] ns = new int[] {1,2,3,4,5};
Arrays.stream(ns);
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:
Arrays.stream(ns).map(n -> n * 2);
Arrays.stream(ns).filter(n -> 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 = Arrays.stream(ns).map(n -> n * 2).filter(n -> n % 4 == 0).toArray();