📜  Java中的 PriorityQueue 比较器() 方法

📅  最后修改于: 2022-05-13 01:55:41.026000             🧑  作者: Mango

Java中的 PriorityQueue 比较器() 方法

Java.util.PriorityQueue.comparator() 方法共享一个重要函数,即设置和返回可用于对 PriorityQueue 中的元素进行排序的比较器。如果队列遵循元素的自然排序模式,则该方法返回空值。
句法:

comp_set = (PriorityQueue)Priority_Queue.comparator()

参数:该方法不带任何参数。
返回值:该方法返回用于按特定顺序对队列元素进行排序的比较器。如果队列遵循默认或自然排序模式,则返回 Null 值。
下面的程序说明了Java.util.PriorityQueue.comparator() 方法:
程序 1:当使用元素的自然排序时:

Java
// Java code to illustrate comparator()
import java.util.*;
 
public class Priority_Queue_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Priority_Queue
        PriorityQueue queue = new PriorityQueue();
 
        // Adding elements to the queue
        queue.add(20);
        queue.add(24);
        queue.add(30);
        queue.add(35);
        queue.add(45);
        queue.add(50);
 
        System.out.println("Priority queue values are: " + queue);
 
        // Creating a comparator
        Comparator comp = queue.comparator();
 
        // Displaying the comparator values
        System.out.println("Since the Comparator value is: " + comp);
        System.out.println("it follows natural ordering");
    }
}


Java
// Java code to illustrate the use of comparator()
import java.util.Comparator;
import java.util.PriorityQueue;
 
class The_Comparator implements Comparator {
    public int compare(String str1, String str2)
    {
        String first_Str;
        String second_Str;
        first_Str = str1;
        second_Str = str2;
        return second_Str.compareTo(first_Str);
    }
}
 
public class Priority_Queue_Demo {
    public static void main(String[] args)
    {
        PriorityQueue queue = new
        PriorityQueue(new The_Comparator());
 
        queue.add("G");
        queue.add("E");
        queue.add("E");
        queue.add("K");
        queue.add("S");
        queue.add("4");
         
 
        System.out.println("The elements with the highest priority element at front of queue"
                           + "order:");
        while(!queue.isEmpty()){
          System.out.print(" "+queue.poll());
        }
    }
}


输出:
Priority queue values are: [20, 24, 30, 35, 45, 50]
Since the Comparator value is: null
it follows natural ordering

程序 2:使用特定比较器时。

Java

// Java code to illustrate the use of comparator()
import java.util.Comparator;
import java.util.PriorityQueue;
 
class The_Comparator implements Comparator {
    public int compare(String str1, String str2)
    {
        String first_Str;
        String second_Str;
        first_Str = str1;
        second_Str = str2;
        return second_Str.compareTo(first_Str);
    }
}
 
public class Priority_Queue_Demo {
    public static void main(String[] args)
    {
        PriorityQueue queue = new
        PriorityQueue(new The_Comparator());
 
        queue.add("G");
        queue.add("E");
        queue.add("E");
        queue.add("K");
        queue.add("S");
        queue.add("4");
         
 
        System.out.println("The elements with the highest priority element at front of queue"
                           + "order:");
        while(!queue.isEmpty()){
          System.out.print(" "+queue.poll());
        }
    }
}
输出
The elements with the highest priority element at front of queueorder:
 S K G E E 4