Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:56:18+00:00 2026-06-12T23:56:18+00:00

Hello Im a bit lost n the priority queues and comparator. I dont really

  • 0

Hello Im a bit lost n the priority queues and comparator.
I dont really see how to make a comparator in java
So what I have is giving me a error and what I have read is no help to me
http://www.tutorialspoint.com/java/java_using_comparator.htm
This post game me some ideas but Im still stuck on how to do it
How do I use a PriorityQueue?

What i have is a class that creates an object with a priority, arrival time, and finish time.
I also have a number of priority queues to place them into. When i start I place them into the arrival queue to sort them and then see which one came in first and place that into the queue one. But when I try to add a second one to the arrival queue it fails and throws an exception.
What I want to do first is to add all the processes to the arrival queue and then sort them so the one with the smallest arrival time will be the first one out of the arrival queue and into the queue one.
Thanks for any help with this

    //the comparator
    Comparator<Integer> comparator = new Comparator();
    //priority queues
    //only needs 10 elements to  hold
    PriorityQueue one = new PriorityQueue(10, comparator);
    PriorityQueue two = new PriorityQueue(10, comparator);
    PriorityQueue three = new PriorityQueue(10, comparator);
    PriorityQueue four = new PriorityQueue(10, comparator);
    PriorityQueue arrival = new PriorityQueue(10, comparator);

    //put all processes in arrival queue
    arrival.add(p1);
    arrival.add(p2);
    arrival.add(p3);
    arrival.add(p4);
    arrival.add(p5);
    arrival.add(p6);
    arrival.add(p7);
    arrival.add(p8);
    arrival.add(p9);
    arrival.add(p10);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:56:20+00:00Added an answer on June 12, 2026 at 11:56 pm

    Let’s look at how you’re defining Comparator, because at the moment I don’t think what you’ve written would even compile.

    Comparator is an interface, meaning that you need to define a class that implements it. That is, you need to define a class that has concrete implementations of the methods described by the interface. Here, there’s only one method you need to worry about – compare. (The interface also defines equals, but that’s an odd choice since it’s equal to the one on Object and so every class will implement this by default…)

    The compare method takes two objects of the target type, and decides which one of them comes “before” the other. It returns:

    a negative integer, zero, or a positive integer as the
    first argument is less than, equal to, or greater than the
    second.

    So – you want to compare objects of whatever the class of your p1, p2 instances are (I’ll call it MyClass). That means that you have to define a class:

    class MyComparator implements Comparator<MyClass> {
    
        public int compare(MyClass a, MyClass b) {
            // TODO
        }
    }
    

    We know that the compare method should return a value depending on which of the MyClass arguments comes before the other one. You’ve said in your question that the one that comes first, is the one that has the smallest (i.e. earliest?) arrival time.

    This is actually very easy, because that’s the so-called natural ordering on java.util.Date objects – so you can just compare their arrival times against each other directly, as the result of that comparison is the same as the overall comparison.

    Therefore the implementation of compare can simply be (assuming a sensibly-named accessor method):

    public int compare(MyClass a, MyClass b) {
        return a.getStartTime().compareTo(b.getStartTime());
    }
    

    And there you go! You’ve just defined your own comparator, that will sort MyClass objects by start time ascending. You can use it in the priority queues similarly to what you have already:

    Comparator<MyClass> comparator = new MyComparator();
    PriorityQueue<MyClass> arrival = new PriorityQueue<MyClass>(10, comparator);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello fellow java developers. I'm having a bit of an issue here. I have
So I am trying to run console 64 bit Hello World program. I have
Hello I've already read quite a bit about the CheckBox/ListView problems in Android. So
Sorry as the question is bit lengthy. I have this string Hello how are
Hello cool so I think I have a good question for a bit of
I have written a Hello, world program with JNI. Java calls c program with
Hello i'm learning a bit JSP and i have a problem with the code
Hello I have the following bit of code which seems to work, but I'm
Hello I'm currently self teaching C# and have run into a bit of a
Hello I am writing a GUI application on Java 1.6 with Swing. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.