📜  优先级队列 java 比较器 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:04.756000             🧑  作者: Mango

代码示例1
// Java program to demonstrate working of 
// comparator based priority queue constructor
import java.util.*;
  
public class Example {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        // Creating Priority queue constructor having 
        // initial capacity=5 and a StudentComparator instance 
        // as its parameters
        PriorityQueue pq = new 
             PriorityQueue(5, new StudentComparator());
                  
             
        } 
    }
} 
  
class StudentComparator implements Comparator{
              
            // Overriding compare()method of Comparator 
                        // for descending order of cgpa
            public int compare(Student s1, Student s2) {
                if (s1.cgpa < s2.cgpa)
                    return 1;
                else if (s1.cgpa > s2.cgpa)
                    return -1;
                                return 0;
                }
        }