Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

priority queue java comparator

// 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<Student> pq = new 
             PriorityQueue<Student>(5, new StudentComparator());
                  
             
        } 
    }
} 
  
class StudentComparator implements Comparator<Student>{
              
            // 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;
                }
        }
  
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #priority #queue #java #comparator
ADD COMMENT
Topic
Name
8+5 =