I need to save some Words in a TreeSet and print them depending on thier length where words with smaller length come first and if 2 Words have the same length, then both should be printed in any order, but my code skips the next word with the same length and prints only the first word with that length.
import java.util.*;
public class Test {
static Comparator<String> comparator = Comparator.comparing(s -> s.length());
public static void main(String[] args) {
Set<String> buch = new TreeSet <>(comparator);
buch.addAll(List.of("a", "test", "ab", "z", "lol", "aaa"));
buch.forEach(System.out::println);
}
}