user3590149
user3590149

Reputation: 1605

JAVA - Priority Queue Compare Double Descending Order

I have a working class that can order in ascending order but if I switch the logic around my results are not what I'm expecting. Maybe I don't understand how the Comparator works. If I use poll to print out my queue then I lose that data. How could I print it out without losing the data? Currently i make a temp queue and reinsert into the old queue.

public class Example
{
    private static Logger log   = LogClass.getLog();

    private PriorityQueue<Double> integerPriorityQueue;

    void inputIntoQueue(Collection<Double> queueNumbers)
    {
        Comparator<Double> comparator = new DoubleComparator();
          integerPriorityQueue = new PriorityQueue<Double>(comparator);
          integerPriorityQueue.addAll(queueNumbers);

          Iterator<Double> it = integerPriorityQueue.iterator();

         log.trace("Priority queue values are: ");

          while (it.hasNext())
              {
                log.trace( it.next()); 
              }      
    }   
}

 class DoubleComparator implements Comparator<Double>
{

    @Override
    public int compare(Double arg0, Double arg1)
        {
                if (arg1 < arg0)
                {
                    return -1;
                }
                if ( arg1  > arg0 )
                {
                    return 1;
                }
                return 0;
        }
}

Update to my code

public class Example
{
    private static Logger              log                 = LogClass.getLog();
    private DoubleComparatorDescending  comparator         = new DoubleComparatorDescending();
    private PriorityQueue<Double>      doublePriorityQueue  = new PriorityQueue<Double>(
                                                                   comparator);
    private PriorityQueue<Double>      tempQueue           = new PriorityQueue<Double>(
                                                                   comparator);

    public PriorityQueue<Double> getDoublePriorityQueue()
        {
            return doublePriorityQueue;
        }


    void inputIntoQueue(Collection<Double> queueNumbers)
        {
            doublePriorityQueue.addAll(queueNumbers);
            printQueue(doublePriorityQueue);
        }

    private void printQueue(PriorityQueue<Double> integerPriorityQueue2)
        {
            log.trace("Priority queue values are: ");

            while (doublePriorityQueue.size() > 0)
                {
                    Double tempDouble = doublePriorityQueue.poll();
                    log.trace(tempDouble);
                    tempQueue.add(tempDouble);
                }
            doublePriorityQueue = tempQueue;
        }
}

class DoubleComparatorDescending implements Comparator<Double>
{
    @Override
    public int compare(Double arg0, Double arg1)
        {
            if (arg1 < arg0)
                {
                    return -1;
                }
            if (arg1 > arg0)
                {
                    return 1;
                }
            return 0;
        }
}

Upvotes: 1

Views: 6348

Answers (2)

Subba Reddy
Subba Reddy

Reputation: 1

Also, as per the javadoc, The Iterator provided in method PriorityQueue.iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Hence,The iterator does not return the elements in any particular order.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

The elements returned by a PriorityQueue's iterator are not ordered using the queue's order.

The javadoc says:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order.

The only guarantee is that the head of the queue, returned by peek() or poll(), is the smallest of all the elements in the queue.

If you want a sorted collection, use a TreeSet or a List that you sort.

Upvotes: 2

Related Questions