While learning lambda's, I found sorted method is not working as intended.
The following program is giving [1, 33, 2, 4, 5, 11]
as an output, however I am expecting a sorted output.
Strange thing is when I replace the [33, 2, 1, 4, 5, 11]
with [ 3, 1,2, 4, 5]
every thing works fine and I got the sorted output as expected.
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(33, 2, 1, 4, 5, 11);
Set<Integer> set = list.stream().sorted().collect(Collectors.toSet());
System.out.println(set);
}
}
Please help me understand this behavior.