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 8770557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:34:46+00:00 2026-06-13T17:34:46+00:00

This seems like a very basic question, but I’ve been stuck for hours. When

  • 0

This seems like a very basic question, but I’ve been stuck for hours.

When do we use methods enqueue/ dequeue & when do we use offer/ poll?!

I want to create a PQ of integers with the void enqueue(int x, int p) and int dequeue() methods, how do I declare such a queue?

Thanks.

  • 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-13T17:34:47+00:00Added an answer on June 13, 2026 at 5:34 pm

    I am assuming that “PQ” means “priority queue”. I’ve never used such a queue (my mental image of a queue is that of a strictly FIFO structure), but after reading documentation, I think you can do this:

    First, you need to create the class of the object you want to store in the queue. Assuming int contents and int priority:

    public class MyClass implements Comparable<MyClass> {
        private int x, p;
    
        /*
         * x: Contents
         * p: Priority
         */
        public MyClass(int x, int p) {
            this.x = x;
            this.p = p;
        }
    
        @override
        public int compareTo(MyClass o) {
            return this.p - o.p;
        }
    
        public int getX() {
            return x;
        }
    }
    

    Now, create your priority queue. If I understood correctly the class documentation, it will use the compareTo method to sort your objects:

    ....
    PriorityQueue<MyClass> pq = new PriorityQueue<MyClass>();
    ....
    pq.add(new MyClass(x, p));
    ....
    

    Check: http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

    Java queues don’t have enqueue and dequeue methods; these operations are done using the following methods:

    • Enqueuing:
      • add(e): throws exception if it fails to insert the object
      • offer(e): returns false if it fails to insert the object
    • Dequeuing:
      • remove(): throws exception if the queue is empty
      • poll(): returns null if the queue is empty
    • Take a look to the first object in the queue:
      • element(): throws exception if the queue is empty
      • peek(): returns null if the queue is empty

    And now, finally: when to use offer and add?

    About offer and add: Well, that depends on how do you want to handle the failure of the insertion in the queue:

    The add method, which Queue inherits from Collection, inserts an element unless it would violate the queue’s capacity restrictions, in which case it throws IllegalStateException. The offer method, which is intended solely for use on bounded queues, differs from add only in that it indicates failure to insert an element by returning false.

    (see: http://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html)

    Hope this helps you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a very basic question but I couldn't find any help on
This may seem like a very basic question, but its been in my head
this is might be a very basic question, but seems like it's not easy
This seems to me like a very simple question, but I can't seem to
This may seem like a very basic question, well, it is, but I am
I feel like this is a very basic CSS question: There seems to be
I understand that this may seems a very basic question, but I always thought
This seems like it should be something very easy to do, but every time
This seems like it should be very simple but I can't get it to
This might seem like a very easy question for some of you folks, but

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.