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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:22:36+00:00 2026-06-14T12:22:36+00:00

What is the use of having element() and remove() in Queue interface when we

  • 0

What is the use of having element() and remove() in Queue interface when we have peek() and poll()?

I checked the docs and found these methods are present in java 7 also.
The only difference mentioned is that element() and remove() throws an exception for empty queue.
We can manually throw an exception (in case we need that) if the queue is empty.

Was it truly necessary to keep both set of methods for this only difference? And if we start making different methods based on such differences I think java classes and interfaces will get filled with a LOT of methods. And is this a true object oriented style?

EDIT: Just to make my question clear, so I don’t get the same “where you need exception use this, otherwise use that” answers.
What I can’t understand is there are many such methods, where having an extra method with the same difference will be useful. So why its implemented only in some cases and not in others? Why the same principle was not applied overall? I can understand that maybe only the language creators can answer these why. I want to know does this approach tally with OOPs principles?

  • 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-14T12:22:37+00:00Added an answer on June 14, 2026 at 12:22 pm

    Was it necessary? …Possibly. Maybe, maybe not.

    In general, you should prefer element() or remove() if the queue should never be empty at this point in the program. That’s part of the general principle of failing fast: if something is wrong, throw an error as soon as possible so it can be debugged. (If your program doesn’t throw up as soon as a bug happens, then it gets significantly harder to trace that bug, because you only find out about it later on at a different place in the stack.)

    That said, when you don’t know if the queue is empty, and you want to find out, then exceptions have potentially unacceptable overhead, and returning null is probably preferable.

    This was clearly a judgement call, and I can imagine that a different decision might have been made, but I think the call that was made is at least justifiable. There are good reasons to prefer element() to peek() in some cases, and good reasons to prefer peek() to element() in other cases.

    In terms of “why is this approach used in some cases and not others”…the answer varies, and it’s always a highly subjective judgement call. In many cases, the JDK frequently returns null on “failure” when null could never be a “positive” answer, and throws exceptions otherwise — for example, NavigableMap.firstKey() can return null because null is the first key, so it throws a NoSuchElementException on an empty map, while NavigableMap.firstEntry() returns null on an empty map because there’s no possibility of confusing null with a valid return.

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

Sidebar

Related Questions

I am having trouble to use the regular expression in between the unique element
Well, I am trying to use an element of function list.But i am having
I have the misfortune of having use conio.h in vc++ 6 for a college
I'm having a problem with the combination of these features of jQuery. The element
I'm having a problem removing an XML element from my doc. I have searched
I am looking for the SQL syntax to use HAVING in the following statement:
Is there a way to use the HAVING clause in some other way without
How to use lexer rules having same starting? I am trying to use two
How can I use Nokogiri with having html entities (like German umlauts) untouched? I.e.:
I know Apple is big on having you use NS objects instead of true

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.