1

I have these class

public class Datos {
private String Nombre;
private String Telefono;
private int Prioridad;


public Datos(String Nombre, String Telefono, int Prioridad)
{
    this.Nombre = Nombre;
    this.Telefono = Telefono;
    this.Prioridad = Prioridad;

}

public String getNombre() {
    return Nombre;
}

public String getTelefono() {
    return Telefono;
}

public int getPrioridad() {
    return Prioridad;
}

public void setNombre(String Nombre) {
    this.Nombre = Nombre;
}

public void setTelefono(String Telefono) {
    this.Telefono = Telefono;
}

public void setPrioridad(int Prioridad) {
    this.Prioridad = Prioridad;
}

}

And i want to accommodate the customers with the Priority. We have 4 categories 1,2,3,4 and i want to acommodate with the PriorityQueue

1 Answer 1

2

You'll want to make your class Datos implement Comparable. This tells java that the objects can be compared. Then define a compareTo method in Datos. This method should return a number > 0 if this > d, equal to 0 if this == d, and < 0 if this < d:

public int compareTo(Datos d) {
    return priority - d.priority;
}

You can then declare a new PriorityQueue<Datos> and add the objects in.

Sign up to request clarification or add additional context in comments.

4 Comments

Where i remote call the method compareTo ? I don't know where. It's my fisrt time to use PriorityQueue
@SaulRamirez You don't have to call compareTo yourself. The PriorityQueue will call that to find what order to sort the objects in.
The System have and exception "proyectosegundoqueue.Datos cannot be cast to java.lang.Comparable" at at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652) at java.util.PriorityQueue.siftUp(PriorityQueue.java:647) at java.util.PriorityQueue.offer(PriorityQueue.java:344) at java.util.PriorityQueue.add(PriorityQueue.java:321)
do you have public class Datos implements Comparable<Datos>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.