Skip to main content

Posts

Showing posts with the label Stream Collectors

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…