'Confused over compareTo implementation with PriorityQueue

I am trying to write a program where I replicate a registrar's office using a priority queue. The sorting for this priority queue would be based on a higher GPA being served first etc. I have constructed a program that does this with my own implementation but I am simply confused how to implement my compareTo method.

//Create Priority Queue
PriorityQueue<Student> pQueue = new PriorityQueue<Student>(10, compareTo());

First I made my priority queue. According to documentation, I can specify what I am to use for my comparisons, compareTo().

But when I tried to implement my compareTo method, it does not allow me to provide any parameters.

private Comparator<? super Student> compareTo(Student x, Student y) {
}

It tells me 'compareTo in BursarOfficeJavaService cannot be applied to ()' and my IDE offers to remove my parameters. But I do not understand how I can make comparisons without objects to compare?

My only other method in the class is recieveEvent, which basically just determines if I am removing from the queue or adding to the queue. The data under the event is a student object I can compare with, but I simply don't understand how to put this all together.

    public void receiveEvent(Event<Student> event) {
    if (event.getType() == EventType.REQUEST_COMPLETED){
        pQueue.poll();
    }

    else{


    }
}

With some help, I have come up with this compare method. However an issue still persists when a new student is checked against the student first on the list, and their GPA is not higher, it will append that student to the end of the list without checking against the other students in the pQueue.

PriorityQueue<Student> pQueue = new PriorityQueue<Student>(10, new Comparator<Student>(){
    @Override
    public int compare(Student o1, Student o2){
        if (o1.equals(o2))
            return 0;
        else if(o1.getGpa() > o2.getGpa())
            return -1;
        else
            return 1;
    }

});

private boolean equals (Student o1, Student o2){
    if (o1.getGpa() == o2.getGpa())
        return true;
    else
        return false;
}


Solution 1:[1]

Create new Class which implements Comparator Inteface and pass this class instance to your priority queue.

If your GPA is Class/Reference Type:(e.g. Integer,Double,Float) you can use following code :

PriorityQueue<Student> pQueue = new PriorityQueue<Student>(10, new Comparator<Student> (){

    @Override
    public int compare(Student arg0, Student arg1) {
        // TODO Auto-generated method stub
        return arg0.getGPA().compareTo(arg1.getGPA());
    }
});

If your GPA is primitive Type:(e.g. int,double,float) you can use following code :

@Override
    public int compare(Student arg0, Student arg1) {
        // TODO Auto-generated method stub
        return Integer.compare(arg0.getGPA(),arg1.getGPA());
    }

Note: Replace Integer according to your datatype.

compareTo() doesn't necessarily return only -1, 0 or 1. compareTo()return 0 for both objects are equal, value less than zero if obj1 < obj2 and value greater than zero if obj1 > obj2. This method is inline comparator declaration.

Solution 2:[2]

The second parameter expects a Comparator object, not a direct call to the compareTo Method:

//Create Priority Queue
PriorityQueue<Student> pQueue = new PriorityQueue<Student>(10, new Comparator<Student>() {

        @Override
        public int compare(Student o1, Student o2) {
            // your logic
        }
    });

The "compare(T o1, T o2)"-Method is part of that Comparator object and has to be defined in one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 OH GOD SPIDERS