PriorityQueue类的compare()方法 (PriorityQueue Class comparator() method)
comparator() method is available in java.util package.
比较器()方法在java.util包中可用。
comparator() method is to get the Comparator object used to order the objects.
compareer()方法是获取用于对对象进行排序的Comparator对象。
comparator() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
比较器()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
comparator() method does not throw an exception at the time of returning comparator.
返回比较器时,compareor ()方法不会引发异常。
Syntax:
句法:
public Comparator comparator();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is Comparator, it gets Comparator object used to order PriorityQueue elements otherwise it returns null when it follows default or natural sorting.
该方法的返回类型为Comparator ,它获取Comparator对象,该对象用于对PriorityQueue元素进行排序,否则在遵循默认或自然排序时返回null。
Example:
例:
// Java program to demonstrate the example
// of Comparator comparator() method of
// PriorityQueue
import java.util.*;
public class ComparatorOfPriorityQueue {
public static void main(String args[]) {
// Instantiate PriorityQueue
PriorityQueue < String > pq = new PriorityQueue < String > ();
// By using add() method is add
// the given element into priority
// queue
pq.add("C");
pq.add("C++");
pq.add("JAVA");
pq.add("PHP");
pq.add("ANDROID");
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
// By using comparator() method is
// used to order the priority queue
// elements
Comparator com = pq.comparator();
// Display Updated PriorityQueue
System.out.println("pq.comparator(): " + com);
}
}
Output
输出量
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]
pq.comparator(): null
翻译自: https://www.includehelp.com/java/priorityqueue-comparator-method-with-example.aspx