Skip to main content

Posts

Showing posts with the label Stream Actions

Java 8 - Streams - Actions - Reduce

reduce operation on stream provides a way to accumulate the result of an operation which traverses all items in the stream.The traversal starts from the leftmost item in the stream.There are three implementations of the reduce operation.The first implementation uses the first item in the stream as initial accumulator.The second implementation expects an initial accumulator input.The third implementation provides a way to return an accumulator which has a different type from the stream items. This is similar to foldLeft in other functional languages e.g. Scala.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

int stream one to two
IntStream oneToTwo = IntStream.of(1, 2);

string stream
Stream stringStream = Stream.of("a", "ab", "abc", "abcd");



USAGE

log.info("first implementation - reducing oneToThree Integer stream by addition: " + oneToThree.reduce((acc, item) -> acc+item …

Java 8 - Streams - Actions - None Match

noneMatch operation on stream returns true if no item in stream satisfies predicate function.
one to three stream IntStream oneToThree = IntStream.of(1, 2, 3);

none match predicate
BiFunction<Stream<Integer>, Predicate<Integer>, Boolean> noneMatch = (stream, predicate) -> stream.noneMatch(predicate);


USAGE

log.info("no number in oneToThree Integer stream is greater than three: " + noneMatch.apply(oneToThree.boxed(), number -> number > 3) );

Java 8 - Streams - Actions - Min

min operation on stream returns an optional of the minimum item in the stream.Another implementation of min operation returns an optional of the minimum item in the stream with respect to a comparator function.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

int stream one to two
IntStream oneToTwo = IntStream.of(1, 2);

order by least number comparator
Comparator<Integer> orderByLeastNumber = (n1, n2) -> n1.compareTo(n2);



USAGE
log.info("obtaining the min item in oneToThree stream: " + oneToThree.min());

log.info("obtaining min item in oneToTwo stream using a comparator: " + oneToTwo.boxed().min(orderByLeastNumber));

Java 8 - Streams - Actions - Max

max operation on stream returns an optional of the maximum item in the stream.Another implementation of max operation returns an optional of the maximum item in the stream with respect to a comparator function.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

int stream one to two
IntStream oneToTwo = IntStream.of(1, 2);


order by greater number comparator
Comparator<Integer> orderByGreaterNumber = (n1, n2) -> n1.compareTo(n2);



USAGE

log.info("obtaining the max item in oneToThree stream: " + oneToThree.max());

log.info("obtaining max item in oneToTwo stream using a comparator: " + oneToTwo.boxed().max(orderByGreaterNumber));



Java 8 - Streams - Actions - For Each Ordered

forEachOrdered operation on stream applies a consumer function to each item in a parallel stream, so that item ordering is not affected by parallel processing.
one to three parallel stream IntStream oneToThreeParallel = IntStream.of(1, 2, 3).parallel();

integer consumer
IntConsumer integerConsumer = i -> log.info(""+i);



USAGE

log.info("applying forEachOrdered operation to oneToThree parallel stream");
oneToThreeParallel.forEachOrdered(integerConsumer);

Java 8 - Streams - Actions - For Each

forEach operation on stream applies a consumer function to each item in the stream.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

integer consumer
IntConsumer integerConsumer = i -> log.info(""+i);



USAGE

log.info("applying forEach operation to oneToThree stream");
oneToThree.forEach(integerConsumer);

Java 8 - Streams - Actions - Find First

findFirst operation on stream returns an optional of the first item in the stream.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);


USAGE

log.info("finding first item in oneToThree stream: " + oneToThree.findFirst());

Java 8 - Streams - Actions - Find Any

findAny operation on stream returns an optional of the first item in the stream.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);



USAGE

log.info("finding any item in oneToThree stream: " + oneToThree.findAny());

Java 8 - Streams - Actions - Count

count operation on stream returns the number of items in the stream.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);



USAGE

log.info("number of items in oneToThree stream: " + oneToThree.count());

Java 8 - Streams - Actions - Collect

collect operation on stream is used to retrieve the result of a stream operation
int stream one to three
IntStream oneToThree = IntStream.of(1, 2, 3);


USAGE
converting stream transformation result to set
Set resultSet = oneToThree.boxed().filter(number -> number > 2).collect(Collectors.toSet()); 
converting stream transformation result to list List resultList = oneToThree.boxed().filter(number -> number > 2).collect(Collectors.toList());

Java 8 - Streams - Actions - Any Match

anyMatch operation on stream returns true if any item in stream satisfies predicate function.
int stream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

USAGE
BiFunction<Stream<Integer>, Predicate<Integer>, Boolean> anyMatch = (stream, predicate) -> stream.anyMatch(predicate);

log.info("some number in oneToThree Integer stream is greater than two: " + anyMatch.apply(oneToThree.boxed(), number -> number > 2) );

Java 8 - Streams - Actions - All Match

allMatch operation on stream returns true if all items in stream satisfy predicate function.
intStream one to three IntStream oneToThree = IntStream.of(1, 2, 3);

USAGE
BiFunction<Stream<Integer>, Predicate<Integer>, Boolean> allMatch = (stream, predicate) -> stream.allMatch(predicate);

log.info("all numbers in oneToThree Integer stream are greater than zero: " + allMatch.apply(oneToThree.boxed(), number -> number > 0) );