For lists, we use the Collections.sort(List)
method. What if we want to sort a HashSet
?
Java 8 way to sort it would be:
fooHashSet.stream()
.sorted(Comparator.comparing(Foo::getSize)) //comparator - how you want to sort it
.collect(Collectors.toList()); //collector - what you want to collect it to
*Foo::getSize
it's an example how to sort the HashSet of YourItem's naturally by size.
*Collectors.toList()
is going to collect the result of sorting into a List the you will need to capture it with List