I have the following code:
Stream.of(1, 4, 5).collect(Collectors.toSet());
From Javadoc of toSet()
method one can read:
There are no guarantees on the type (...) of the Set returned
I took a look at the actual implementation of toSet()
method and at a first sight it looks like HashSet
is always returned (at least in JDK 11).
I know that implementation can change in the future without violation of the contract but is there currently any situation when different implementation than HashSet
is returned?
No. The JDK source code clearly shows that Collectors.toSet()
always uses HashSet::new
. As you say, that may of course change.