Home > Mobile >  Java Priority Queue Comparator Syntax
Java Priority Queue Comparator Syntax

Time:11-03

Can anyone explain the syntax of the comparison Operator used here? What does it do

PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));

CodePudding user response:

The priority queue takes as input a single parameter keyExtractor which is the function used to extract the integer sort key.

In your case, it means that int[] arrays, which are stored in the priority queue will be sorted by their first element.

This means if you have a queue with arrays like this {[50,2,3],[1,1],[3,7]} they will be sorted like this {[1,1][3,7][50,2,3]} note the 1,3,50 sequence of the first elements, which are the sort keys

More details are available in this article

  • Related