I have below piece of code
Comparator<StudentDTO> compareNames = Comparator.comparing(StudentDTO::getName);
PriorityQueue<StudentDTO> h = new PriorityQueue<>(compareNames);
h.offer(new StudentDTO(5, "c"));
h.offer(new StudentDTO(2, "b"));
h.offer(new StudentDTO(8, "z"));
h.offer(new StudentDTO(1, "a"));
System.out.println(h);
And I am getting output as below:
[StudentDTO [rollNo=1, Name=a], StudentDTO [rollNo=2, Name=b], StudentDTO [rollNo=8, Name=z], StudentDTO [rollNo=5, Name=c]]
Not sure why Name=z is coming before Name=c. Edit: I am using java 8.