Java 8 – How to use Collectors.toCollection Collector with examples
This tutorial explains how to use Java 8's predefined collector returned by
Where,
- only input parameter is
- output is a Collector with result containerClick to Read tutorial explaining collector internals incl. 'result container' as a
Which specific type of subtype
So, when the stream elements reach the
To complete our understanding of how the toCollection Collector works, let us see couple of Java 8 code examples which show the collector in action.
OUTPUT of the above code
Explanation of the code
OUTPUT of the above code
Explanation of the code
Collectors.toCollection()
method with examples. It first explains the definition of the static toCollection()
method, followed by a quick explanation of its working, and then shows how to use Collector returned by Collectors.toCollection()
using two Java 8 code examples.
Collectors.toCollection() method
Collectors.toCollection()
method is defined with the following signature - public static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)
- only input parameter is
collectionFactory
which is an instance of a SupplierClick to Read Tutorial on Supplier Functional Interface functional interface.- output is a Collector with result containerClick to Read tutorial explaining collector internals incl. 'result container' as a
Collection
of type T
.
How the Collector returned by Collectors.toCollection() method works
The collector returned by Collector.toCollection()
method stores(collects) the stream elements in an object of a subtype of java.util.Collection
. Which specific type of subtype
Collection
to use is specified to the collector using the collectionFactory
input parameter which is a Supplier
functional interface. A Supplier
’s task is to supply objects of a given type. In this case, the supplier passed to the toCollection()
method needs to 'supply' a concrete Collection
instance in order for the collector to work.Stream.collect()
method with a toCollection collector, the Collector
invokes the Supplier
, in return gets an object of the Collection
subtype it supplies, and then collects the stream elements into the supplied Collection
object. This Collection
object, containing the collected stream elements, is then returned as the output of the processed stream.To complete our understanding of how the toCollection Collector works, let us see couple of Java 8 code examples which show the collector in action.
Example 1 - Java 8 Code showing Collectors.toCollection() usage
//ToCollectionCollector.java
package com.javabrahman.java8.collector;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class ToCollectionCollector {
static List<Employee> employeeList
= Arrays.asList(new Employee("Tom Jones", 45, 15000.00),
new Employee("Tom Jones", 45, 7000.00),
new Employee("Ethan Hardy", 65, 8000.00),
new Employee("Nancy Smith", 22, 10000.00),
new Employee("Deborah Sprightly", 29, 9000.00));
public static void main(String[] args) {
//Collecting stream elements in a LinkedList
LinkedList employeeLinkedList = employeeList
.stream()
.collect(Collectors.toCollection(LinkedList::new));
System.out.println("No.of employees in employeeLinkedList: " + employeeLinkedList.size());
System.out.println("Employees in employeeLinkedList: " + employeeLinkedList);
}
}
//Employee.java(POJO class)
package com.javabrahman.java8.collector;
public class Employee {
private String name;
private Integer age;
private Double salary;
public Employee(String name, Integer age, Double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
//Setters and Getters for name, age & salary go here
public String toString(){
return "Name: "+ this.name
+ " Age: "+ this.age;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Employee)) {
return false;
}
Employee empObj = (Employee) obj;
return this.age == empObj.age
&& this.name.equalsIgnoreCase(empObj.name);
}
@Override
public int hashCode() {
int hash = 1;
hash = hash * 17 + this.name.hashCode();
hash = hash * 31 + this.age;
return hash;
}
}
No.of employees in employeeLinkedList: 5 Employees in employeeLinkedList: [Name: Tom Jones Age: 45, Name: Tom Jones Age: 45, Name: Ethan Hardy Age: 65, Name: Nancy Smith Age: 22, Name: Deborah Sprightly Age: 29]
ToCollectionCollector
class contains a static list ofEmployee
objects -employeeList
.- In the
main()
method a stream ofEmployee
objects is created usingList.stream()
method. - Stream of employees is then pipelined to the
collect()
terminal operationClick to Read Tutorial explaining intermediate & terminal Stream operations. - To the
collect()
method,Collector
returned byCollectors.toCollection()
method is passed as a parameter. - toCollection collector is invoked with the input “
LinkedList::new
”, which is a method reference Click to Read Tutorial on Java 8's Method References, to theLinkedList
’s default constructor . - toCollection collector then collects the
Employee
objects in the stream in an instance ofLinkedList
and returns this linked list as the output of stream processing. - Collected
LinkedList
is assigned to a parameter namedemployeeLinkedList
. The size of the linked list and its contents are then printed. As expected, the linked list has5
Employee
objects, and all the 5 objects are printed with their name and age.
Employee
class and employeeList
objects with their values remain the same as the previous code usage example and hence are not shown again in example 2 below for brevity.)Example 2 - Collectors.toCollection() in action
public static void main(String[] args) {
//Collecting stream elements in a HashSet
HashSet employeeHashSet = employeeList
.stream()
.collect(Collectors.toCollection(HashSet::new));
System.out.println("No.of employees in employeeHashSet: " + employeeHashSet.size());
System.out.println("Employees in employeeHashSet: " + employeeHashSet);
}
OUTPUT of the above code
No.of employees in employeeHashSet: 4 Employees in employeeHashSet: [Name: Tom Jones Age: 45, Name: Ethan Hardy Age: 65, Name: Deborah Sprightly Age: 29, Name: Nancy Smith Age: 22]
- The
Employee
objects in the stream are collected, with theSupplier
input to thetoCollection()
method being the method reference to the defaultHashSet
constructor -HashSet::new
”. - The collected
Employee
objects are then returned in aHashSet
instance and assigned toemployeeHashSet
variable. - The count of collected employees is then printed along with the
employeeHashSet
contents. - However, this time the number of employee objects in
employeeHashSet
comes out to be4
. - If you observe closely in the output to see which
Employee
object is missing, then you will see that 1 out of 2"Tom Jones"
(employee name) objects is missing. This is however the correct output. This is because, both the "Tom Jones" objects have the samename
andage
, their salary being different. Butequals()
implementation inEmployee
only checks forname
andage
to determine equality. As a result, theHashSet
insertion logic finds these two objects to be equal and only stores one of them, as aHashSet
cannot store duplicate objects by design.
Java 8 Collectors' Tutorials on JavaBrahman
Understanding Basics of Java 8 CollectorsClick to Read Tutorial explaining basics of Java 8 CollectorsCollectors.groupingBy()Click to Read Tutorial on Grouping with CollectorsCollectors.partitioningBy()Click to Read Partitioning using Collectors TutorialCollectors.counting()Click to Read Counting with Collectors Tutorial Collectors.maxBy()/minBy()Click to Read Tutorial on finding max/min with CollectorsCollectors.joining()Click to Read Tutorial on joining as a String using CollectorsCollectors.collectingAndThen()Click to Read Tutorial on collectingAndThen CollectorCollectors.averagingInt() /averagingLong() /averagingDouble()Click to Read Tutorial on Averaging CollectorCollectors.toCollection()Click to Read Tutorial on Collectors.toCollection CollectorCollectors.mapping()Click to Read Tutorial on Mapping Collector
Understanding Basics of Java 8 CollectorsClick to Read Tutorial explaining basics of Java 8 CollectorsCollectors.groupingBy()Click to Read Tutorial on Grouping with CollectorsCollectors.partitioningBy()Click to Read Partitioning using Collectors TutorialCollectors.counting()Click to Read Counting with Collectors Tutorial Collectors.maxBy()/minBy()Click to Read Tutorial on finding max/min with CollectorsCollectors.joining()Click to Read Tutorial on joining as a String using CollectorsCollectors.collectingAndThen()Click to Read Tutorial on collectingAndThen CollectorCollectors.averagingInt() /averagingLong() /averagingDouble()Click to Read Tutorial on Averaging CollectorCollectors.toCollection()Click to Read Tutorial on Collectors.toCollection CollectorCollectors.mapping()Click to Read Tutorial on Mapping Collector