Priority queue in java

0

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;
    }
}
    
asked by gabriel gomez 05.01.2017 в 20:45
source

2 answers

1

The peek () function uses the FIFO algorithm (First in first out) that is to say that the first to enter is the first to leave, in other words it will always return the first item on the list.

In order for the queue to be ordered in a specific way, two parameters must be sent when initializing it:

PriorityQueue(int capacidadInicial, Comparator<? super E> comparador)

Where initial capacity is the capacity that our queue will have and the comparator is a class that includes the comparison algorithm like the following example:

class PQsort implements Comparator<Integer> {  

public int compare(primitivo a, primitivo b){}

  public int compare(Integer primerElemento, Integer segundoElemento) {
      if(primerElemento > segundoElemento)
          return primerElemento;
      else
          return segundoElemento;
  }

}

That class must include a function called compare that returns the type of data to be compared and you must request two of those types of data.

    
answered by 05.01.2017 в 21:06
0

I think you need to fill the queue of elements ... Anyway you can use the constructor that receives the comparator and modify it at your whim ... like for example:

PriorityQueue<Elemento> q = new PriorityQueue(new Comparator<Elemento>() {
            @Override
            public int compare(Elemento o1, Elemento o2) {
                int r = 0;
                if(o1.getAtributo().length() > o2.getAtributo().length()){
                    r = 1;
                }else if (o1.getAtributo().length() < o2.getAtributo().length()){
                    r = -1;
                }
                return r;
            }
        });

If you then iterate through the console you will get this:

hola
feliz
amigo
querido
dosmilcatorce

I hope it helps you!

    
answered by 05.01.2017 в 23:36