Can we have a Priority Queue of an array of integers in java

9.2k Views Asked by At

can we define a Priority Queue of an array that compare arrays by their length?

    PriorityQueue<int[]> pq=new PriorityQueue<int[]>(5, (a,b) -> a.length - b.length);
    int[] a = new int[]{1,2};
    int[] b = {1,2,3};
    int[] c = new int[]{3};
    pq.add(a);
    pq.add(b);
    pq.add(c);
    while (pq.size() != 0)
    {
        System.out.println(pq.remove());
    }
}

output :
[I@1c20c684
[I@1fb3ebeb
[I@548c4f57

1

There are 1 best solutions below

0
On

You can, with whatever comparator you want. Your code is printing addresses of the array as output. You can use java.util.Arrays.toString() method.