當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java PriorityBlockingQueue comparator()用法及代碼示例


PriorityBlockingQueue的comparator()方法返回比較器,該比較器可用於對PriorityBlockingQueue中的元素進行排序。如果隊列遵循元素的自然排序模式,則該方法返回null值。

用法:

public Comparator<? super E> comparator()

返回值:此方法返回用於以特定順序對集合中的元素進行排序的比較器集合。如果PriorityBlockingQueue遵循默認或自然排序模式,則它將返回null值。


下麵的程序說明PriorityBlockingQueue的comparator()方法:

示例1:演示PriorityBlockingQueue上的comparator()方法,該方法包含整數列表。

// Java Program Demonstrate comparator() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of PriorityBlockingQueue 
        PriorityBlockingQueue<Integer> PrioQueue 
            = new PriorityBlockingQueue<Integer>(); 
  
        // Add numbers to PriorityBlockingQueue 
        PrioQueue.put(45815616); 
        PrioQueue.put(4981561); 
        PrioQueue.put(4594591); 
        PrioQueue.put(9459156); 
  
        // get String representation of PriorityBlockingQueue 
        String str = PrioQueue.toString(); 
  
        // Creating a comparator using comparator() 
        Comparator comp = PrioQueue.comparator(); 
  
        // Displaying the comparator values 
        System.out.println("Comparator value: " + comp); 
  
        if (comp == null) 
            System.out.println("PriorityBlockingQueue"
                               + "follows natural ordering"); 
        else
            System.out.println("PriorityBlockingQueue follows"
                               + comp); 
    } 
}
輸出:
Comparator value: null
PriorityBlockingQueuefollows natural ordering

示例2:演示PriorityBlockingQueue上的toString()方法,該方法包含字符串列表。

// Java Program Demonstrate comparator() 
// method of PriorityBlockingQueue 
  
import java.util.concurrent.PriorityBlockingQueue; 
import java.util.*; 
  
// Comparator to compare Strings 
class COMPARING implements Comparator<String> { 
    public int compare(String str1, String str2) 
    { 
        return str2.compareTo(str1); 
    } 
} 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // define capacity of PriorityBlockingQueue 
        int capacityOfQueue = 5; 
  
        // create object of PriorityBlockingQueue 
        // by passing capacity and comparator class 
        // as parameters. 
        PriorityBlockingQueue<String> characters 
            = new PriorityBlockingQueue<String>(capacityOfQueue, 
                                                new COMPARING()); 
  
        // Add Strings 
        characters.add("Geeks"); 
        characters.add("forGeeks"); 
        characters.add("A computer portal"); 
  
        // Getting the comparator using comparator() 
        Comparator comp = characters.comparator(); 
  
        // Displaying the comparator values 
        System.out.println("Comparator value is: " + comp); 
  
        if (comp == null) 
            System.out.println("PriorityBlockingQueue"
                               + "follows natural ordering"); 
        else
            System.out.println("PriorityBlockingQueue follows: "
                               + comp); 
        // display result 
        System.out.println("\nThe elements after custom Comparator"); 
        for (String e : characters) 
            System.out.print(e + ", "); 
    } 
}
輸出:
Comparator value is: COMPARING@28d93b30
PriorityBlockingQueue follows: COMPARING@28d93b30

The elements after custom Comparator
forGeeks, Geeks, A computer portal,

注:本文由純淨天空篩選整理自 PriorityBlockingQueue comparator() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。