Want to understand why PriorityQueue.addAll method is not considering comparator

23 Views Asked by At

I have the below code written to add a collection of Integers to the Priority Queue. I am getting confused on the out put

public static void main(String[] args) {
    int[] pipes = {4, 3, 2, 6 };
    PriorityQueue<Integer> minHeap = new PriorityQueue<>(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1-o2;
        }
    });
    minHeap.addAll(IntStream.of(pipes).boxed().collect(Collectors.toList()));
    System.out.println(minHeap);
}

I expect the output as [2, 3, 4, 6] but it is printed as [2, 4, 3, 6]. Could someone please explain why 3 is not printed before 4?? Am i doing something wrong with addAll method??

0

There are 0 best solutions below