I would like to know what priority this exercise establishes with a priority queue to give that result, the result of removing the element from my head gives me "beloved". How is priority established? Does the compareTo
method have something to do with it?
import java.util.PriorityQueue;
class TestEx_c_ene14 {
public static void main(String args[]) {
PriorityQueue<Elemento> q = new PriorityQueue();
String s[] = {"querido", "amigo", "hola", "feliz", "dosmilcatorce"};
for (int i = 0; i < s.length; i++)
System.out.println(q.peek().toString());
}
}
class Elemento implements Comparable<Elemento> {
String atributo;
public Elemento(String s) {
atributo = s;
}
public String getAtributo() {
return atributo;
}
public String toString() {
return atributo;
}
public int compareTo(Elemento e) {
int r = 0;
if (e.getAtributo().length() < getAtributo().length())
r = +1;
else
if (e.getAtributo().length() > getAtributo().length())
r = -1;
return r;
}
}