-1

I have a class called jobType

package pcs_assignment_2;

public class JobType
{
    private int ID;
    private double aTime;
    private double sTime;

    public JobType(int ID, double aTime, double sTime)
    {
        this.ID = ID;
        this.aTime = aTime;
        this.sTime = sTime;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public double getaTime() {
        return aTime;
    }

    public void setaTime(double aTime) {
        this.aTime = aTime;
    }

    public double getsTime() {
        return sTime;
    }

    public void setsTime(double sTime) {
        this.sTime = sTime;
    }

    public String toString() {
        return "JobType{" + "ID=" + ID + "aTime=" + aTime + "sTime=" + sTime + '}';
    }

}

Now what I wanna have is a PriorityQue of Type JobType which sorts the queue on the basis of sTime. Is it possible?

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
John
  • 4,413
  • 6
  • 25
  • 28
  • 2
    What part of the JavaDocs for [PriorityQueue](http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html) are you having problems understanding? – Brian Roach Sep 11 '11 at 06:49
  • using the comparator.... – John Sep 11 '11 at 06:55
  • possible duplicate of [Java: How do I use a PriorityQueue?](http://stackoverflow.com/questions/683041/java-how-do-i-use-a-priorityqueue) - that has a concrete example of using PriorityQueue with a Comparator – Brian Roach Sep 11 '11 at 06:57

4 Answers4

3

A custom comparator needs to be written, see the code below. here 10 in the initial capacity of the queue.

    new PriorityQueue<JobType>(10, new Comparator<JobType>() {

    @Override
    public int compare(JobType o1, JobType o2) {
             return Double.compare(o1.sTime,o2.sTime); 
    }
    });
Adithya Surampudi
  • 4,354
  • 1
  • 17
  • 17
  • Would the capacity of the que keep increasing as and when objects get added ?? – John Sep 11 '11 at 06:53
  • Yes, it would. Its just an initial capacity, there is no constructor for only comparator. – Adithya Surampudi Sep 11 '11 at 06:54
  • 2
    Creating a `Double` for every comparison is expensive. An alternative is to use `Double.compare(o1.sTime, o2.sTime)` – Peter Lawrey Sep 11 '11 at 07:06
  • Good comment!, made the change – Adithya Surampudi Sep 11 '11 at 07:08
  • @Peter why do you say that creating `Double` for every comparison is expensive? Because of the object creation overhead? – Miserable Variable Sep 11 '11 at 07:16
  • @Hemal Pandya, Yes. It can be 10-100x longer than the comparison itself and creates a lot of garbage if called enough. Note: two objects get created. (the second is auto-boxed) – Peter Lawrey Sep 11 '11 at 07:21
  • @Peter This is an interesting contention. Looking at the code of `Double.compare` it seems that the comparison itself would be quite expensive. Moreover, I believe object creation is cheap and the GC overhead also should be very small for short lived objects. Just to learn, I will ask this question to the community. – Miserable Variable Sep 11 '11 at 07:31
  • Using an object or not can make a big difference in a micro-benchmark. However I tried comparing what you get in this type of situation, with a PriorityQueue and and object wrapper and the difference is much smaller overall. Using the primitive method was about 15% faster for the whole test. – Peter Lawrey Sep 11 '11 at 07:37
  • Thanks Peter. I guess the actual difference will vary depending on the overall usage and make come down ever more in some cases. – Miserable Variable Sep 11 '11 at 07:45
0

Use a PriorityQueue with a Comparator. See http://download.oracle.com/javase/1,5.0/docs/api/java/util/PriorityQueue.html#PriorityQueue(int,%20java.util.Comparator)

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0

Apart from the comparator option, I believe it would be possible to achieve the same behavior by just making your class implement the Comparable interface; do so only if this ordering mimics the natural ordering of your object.

Scorpion
  • 3,938
  • 24
  • 37
-1

You could have a simple List where all the jobs reside in and then before you want to take one out, sort the list using a compare function on the sTime.

Dorpsidioot
  • 474
  • 2
  • 7