Another implementation of the Set interface is LinkedHashSet. LinkedHashSet maintains the order of elements and helps to overcome the HashSet limitation. The following code demonstrates this:
This code is quite similar to the one seen earlier, except that it uses a LinkedHashSet. The input List is passed as a parameter to the LinkedHashSet constructor.
Since a LinkedHasSet is used, the elements will be ordered as in this case the output should be:
5 10 15 20 35 40 25