Skip to main content

Posts

Showing posts with the label Streams

Java 8 - Streams - Collectors - To Set

toSet operation on Collectors provides a way to aggregate the result of a stream operation into a Set.
int stream one to three-three IntStream oneToThree_three = IntStream.of(1, 2, 3, 3);


USAGE

Set<Integer> resultSet = oneToThree_three.boxed().filter(number -> number > 2).collect(Collectors.toSet());

Java 8 - Streams - Collectors - To List

toList operation on Collectors provides a way to aggregate the result of a stream operation into a List.
int stream one to three-three IntStream oneToThree_three = IntStream.of(1, 2, 3, 3);


USAGE

List<Integer> resultList = oneToThree_three.boxed().filter(number -> number > 2).collect(Collectors.toList());

Java 8 - Streams - Collectors - To Collection

toCollection operation on Collection provides a way to aggregate the result of a stream operation into a Collection.
int stream one two three-three IntStream oneToThree_three = IntStream.of(1, 2, 3, 3);


USAGE

TreeSet resultCollection = oneToThree_three.boxed() .filter(number -> number > 2) .collect(Collectors.toCollection(TreeSet::new));

Java 8 - Streams - Collectors - Summing

summing operation on Collectors provides a way to aggregate the result of a stream operation into a number.The different implementations yield Integer, Double and Long which are summingInt, summingDouble, summingLong respectively.
stream of employees Stream employees = Stream.of( new Employee("Joe", "Blogs", "Sales", 100.0), new Employee("Richardo", "Banks", "Engineering", 50.0), new Employee("Hewlet", "Packer", "Warehouse", 40.0) );


USAGE

collecting stream transformation result in a Double

Double summedSalaries = employees .filter(employee -> employee.getLastName().startsWith("B")) .collect(Collectors.summingDouble(Employee::getSalary));

Java 8 - Streams - Collectors - Partition By

partitionBy operation on Collectors provides a way to aggregate the result of a stream operation into a map.The map contains two items which are the result partitions based on a predicate function.
stream of employees Stream employees = Stream.of( new Employee("Joe", "Blogs", "Sales", 100.0), new Employee("Richardo", "Banks", "Engineering", 50.0), new Employee("Hewlet", "Packer", "Warehouse", 40.0) );


USAGE

Map<Boolean, List<Employee>> employeePartitions = employees .collect(Collectors.partitioningBy(employee -> employee.getSalary() > 50));

Java 8 - Streams - Collectors - Joining

joining operation on Collectors provides a way to aggregate the result of a stream operation into a concatenated string.There are two implementations. The first simply concatenates the stream items, while the second concatenates the items with a delimiter.
stream of employees Stream employees = Stream.of( new Employee("Joe", "Blogs", "Sales", 100.0), new Employee("Richardo", "Banks", "Engineering", 50.0), new Employee("Hewlet", "Packer", "Marketing",40.0) );

another stream of employees
Stream employees_2 = Stream.of( new Employee("Ruben", "Paul", "Sales", 100.0), new Employee("Shelly", "Smith", "Engineering", 50.0) ); 



USAGE

collecting stream of transformation result in a concatenated string
String LastNames = employees .map(employee -> employee.getLastNa…

Java 8 - Streams - Collectors - Grouping By

groupingBy operation on Collectors provides a way to aggregate the result of a stream operation into a map, where the map contains items whose keys are the groupings.There are two implementations, the first implementation performs on-stage grouping, the second performs two-stage grouping (grouping and reduction)
stream of employees Stream<Employee> employees = Stream.of( new Employee("Joe", "Blogs", "Sales", 100.0), new Employee("Richardo", "Banks", "Engineering", 50.0), new Employee("Hewlet", "Packer", "Warehouse", 40.0), new Employee("Shelly", "Cooper", "Warehouse", 40.0) );

another stream of employees
Stream<Employee> employees_2 = Stream.of( new Employee("Joe", "Blogs", "Sales", 100.0), new Employee("Richardo", "Banks", "Engineering&q…

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);

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);