Collecting to a Set The method toSet() creates a collector that uses a mutable container of type Set to perform…