Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority Queue of an array of integers in java

I want to compare the second element of the following array:

int[][] intervals = new int[][]{new int[]{0, 30},new int[]{5, 10},new int[]{15, 20}};

My priority queue with custom comparator:

PriorityQueue<int[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);

But I get the below 2 errors:

Line 8: error: array required, but Object found
        PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
                                                                                       ^
Line 8: error: array required, but Object found
        PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
                                                                                             
like image 929
Suhas Ramesh Avatar asked Jun 06 '26 00:06

Suhas Ramesh


1 Answers

You could use Integer.compare

PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a,b) -> Integer.compare(a[1],b[1]));
like image 97
Happy Coder Avatar answered Jun 08 '26 13:06

Happy Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!