Skip to main content

Posts

Showing posts with the label Stream Transformations

Java 8 - Streams - Transformations - Sorted

sorted operation on stream returns a new stream whose items are sorted.There are two implementations, the first sorts the stream items in natural order and the second sorts the stream items using a comparator.
int stream three to one Stream<Integer> threeToOne = Stream.of(3, 2, 1);

int stream two to one
Stream<Integer> twoToOne = Stream.of(2, 1);




USAGE

Stream<Integer> oneToThree_sorted = threeToOne.sorted();

Stream<Integer> twoToOne_sorted_comparator = twoToOne.sorted((a,b) -> a.compareTo(b));




Java 8 - Streams - Transformations - Skip

skip operation on stream returns a new stream whose items are unskipped in the original stream.
int stream one to three Stream<Integer> oneToThree = Stream.of(1, 2, 3);


USAGE
// Skips first item and returns Stream.of(2,3)
Stream<Integer> oneToThree_skipped = oneToThree.skip(1);


Java 8 - Streams - Transformations - Peek

peek operation on stream returns a stream similar to the original stream, while applying a Consumer function to each item in the stream.
int stream one to three Stream<Integer> oneToThree = Stream.of(1, 2, 3);



USAGE

Stream<Integer> oneToThree_peeked = oneToThree.peek(i -> log.info(i.toString()));

log.info("triggering stream processing with a terminal action");
oneToThree_peeked.count();




Java 8 - Streams - Transformations - Map

map operation on a stream returns a new stream whose items are transformed.There is also mapToInt, mapToDouble and mapToLong which yield IntStream, DoubleStream and LongStream respectively.
int stream one to three Stream<Integer> oneToThree = Stream.of(1, 2, 3);

int stream one to two
Stream<Integer> oneToTwo = Stream.of(1, 2);


USAGE

Stream<Integer> oneToThree_transformed = oneToThree.map(i -> i + 1);

DoubleStream oneToTwo_transformed = oneToTwo.mapToDouble(i -> i.doubleValue());

Java 8 - Streams - Transformations - Limit

limit operation on stream returns a new stream whose item count is limited with respect to the original stream.
int stream one to three Stream<Integer> oneToThree = Stream.of(1, 2, 3);


USAGE
Stream<Integer> limitedStream = oneToThree.limit(2);

log.info("verifying that oneToThree integer stream limited by 2 returns a stream with 2 items: ");

limitedStream.forEach(number -> log.info(number.toString()));




Java 8 - Streams - Transformations - Flat Map

when a transformation on a stream results in a stream of stream, we use flatMap to flatten the result back to a stream.There is also flatMapToInt, flatMapToDouble and flatMapToLong which yield IntStream, DoubleStream and LongStream respectively.
integer stream one to three Stream<Integer> oneToThree = Stream.of(1, 2, 3);

integer stream one to two
Stream<Integer> oneToTwo = Stream.of(1, 2);

integer to stream function
Function<Integer, Stream> integerToStreamFunction = i -> IntStream.range(i, 5).boxed();

integer to double stream function
Function<Integer, DoubleStream> integerToDoubleStreamFunction = i -> DoubleStream.of(i);



USAGE

log.info("verifying that flatMap operation flattens a stream of integer stream to an integer stream: ");

oneToThree.flatMap(number -> integerToStreamFunction.apply(number)).forEach(number -> log.info(number.toString()));


log.info("verifying that flatMapToDouble operation flattens a stream of integer stream t…

Java 8 - Streams - Transformations - Filter

filter operation on stream returns a new stream whose items satisfy the filter predicate function.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);


USAGE

IntStream oneToThreeFiltered = oneToThree.filter(number -> number > 2);