Saul Ramirez
Saul Ramirez

Reputation: 33

How i use PriorityQueue in Java with class ?

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

Upvotes: 1

Views: 45

Answers (1)

Vincent
Vincent

Reputation: 484

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.

Upvotes: 2

Related Questions