It's the same as the first expression except that you don't keep a reference to the newly created set in your context. If you're not going to need the set's value after the allMatch
invocation, it's the same. It's essentially a method reference expression with the newly created instance of HashSet
. While it might be confusing at first sight, a HashSet
is only created once, then the method reference bound to this newly created instance and used as such in the evaluation of the allMatch
operation.
While it might be a working solution, it can be dangerous, especially with non-sequential (parallel) stream pipelines because it violates the allMatch
predicate's statelessness requirement in the API contract.