Stream to LinkedHashSet

You simply don't know the specific type created by that factory method (it guarantees to return Set, nothing else).

The only reliable way is to actually control what happens, by ending the stream operation with

... collect( Collectors.toCollection( LinkedHashSet::new ) );

The .collect(Collectors.toSet()) does not create a LinkedHashSet, but rather a regular HashSet. Rather use .collect(Collectors.toCollection(LinkedHashSet::new) to ensure that a LinkedHashSet is being used.


Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

See https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#iterator()

Use List rather than Set.