Curious_Bop
Curious_Bop

Reputation: 311

Java iterate through priority queue

I'm trying to do a while loop that will iterate through a java priority queue and read in top of the queue's date. One it has the date value, looks through the rest of the queue to see if this date is being used elsewhere, if so add these elements temporarily to their own queue so I can call a different comparator method to sort them out.

public JobRequest closestDeadlineJob(int freeCPUS) {
    // find top job to determine if other jobs for date need to be considered
    JobRequest nextJob = scheduledJobs.peek(); // return top most job
    // what is it's date?
    Date currentDate = nextJob.getConvertedDeadlineDate();
    JobPriorityQueue schedulerPriorityQueue = new JobPriorityQueue();
    schedulerPriorityQueue.addJob( nextJob );

    while(true) {


    }

    // this is the item at the top of the PRIORTY JOB queue to return 
    // remove that item from scheduledJobs
   // return null; // replace with to the one you want to return
}

what I have so far, as you can see not very much

Upvotes: 1

Views: 5642

Answers (2)

mik1
mik1

Reputation: 568

PriorityQueue does not provide you a sorted iteration order. The only guarantee of a PriorityQueue is that extraction methods (peek/poll/remove) will return a smallest element in the set according to your Comparator. If you need a sorted iteration order - use TreeSet/TreeMap instead.

Upvotes: 3

Kanagavelu Sugumar
Kanagavelu Sugumar

Reputation: 19260

import java.util.Date;
import java.util.Comparator;
import java.util.PriorityQueue;

class Job implements Runnable{

 Priority priority;
 Date dateOccurance;

 public Job(Priority priority, Date occurance){
    this.priority = priority;
    this.dateOccurance = occurance;
 }

 public void run(){
    //Job execution
    System.out.println("executed");
 }
}

enum Priority {
 High,
 Medium,
 Low
}

class JobComparator implements Comparator<Job>  {

 @Override
 public int compare(Job j1, Job j2) {

    if(j1.priority.ordinal() > j2.priority.ordinal()) {
        return 1;
    } else if (j1.priority == j2.priority) {
        if(j1.dateOccurance.after(j2.dateOccurance)) {
            return 1;
        } else if (j1.dateOccurance.before(j2.dateOccurance)) {
            return -1;
        } else {
            return 0;
        }
    }  
    return -1;
 }

}

public class PriorityQueueTest {

public static void main(String[] args) throws InterruptedException {
    Date d = new Date();
    Job job1 = new Job(Priority.High, d);
    Job job2 = new Job(Priority.High, d);
    Job job3 = new Job(Priority.Medium, d);
    Job job4 = new Job(Priority.Low, d);    
    Thread.sleep(2000);
    Date l = new Date();
    Job job5 = new Job(Priority.Low, l);    

    Comparator<Job> jComp = new JobComparator();
    PriorityQueue<Job> queue = 
        new PriorityQueue<Job>(10, jComp);

    queue.add(job4);
    queue.add(job3);
    queue.add(job1);
    queue.add(job2);
    queue.add(job5);


    while (queue.size() != 0)
    {
        Job j = queue.remove();
        System.out.println(j.priority +"     "+j.dateOccurance);
    }
 }      

}

Upvotes: 1

Related Questions