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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:45:40+00:00 2026-05-26T14:45:40+00:00

Why is calling containsKey on a HashMap slower then get ? Test: http://ideone.com/QsWXF (>15%

  • 0

Why is calling containsKey on a HashMap slower then get?

Test: http://ideone.com/QsWXF (>15% difference, run on sun-jdk-1.6.0.17)

  • 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-05-26T14:45:40+00:00Added an answer on May 26, 2026 at 2:45 pm

    Because it does [ever so slightly] more work, see the OpenJDK 7 source.


    Note that containsKey calls getEntry while get directly “does the magic lookup”. I do not know why it was done this way, and am further puzzled by the use/not use of getForNullKey: See John B’s and Ted Hopps’s comments as to why this is done.

    get has an early code split for a null-key (note that get will return null if the entry doesn’t exist or existed with a null value stored):

    315           if (key == null)
    316               return getForNullKey();
    ...
    322               if (e.hash == hash &&
                          ((k = e.key) == key || key.equals(k)))
    323                   return e.value;
    

    While getEntry, called from containsKey, does not split to getForNullKey and there is additional work here to check for the null-key case (for each Entry scanned in the chain):

    366               if (e.hash == hash &&
    367                   ((k = e.key) == key || (key != null && key.equals(k))))
    368                   return e;
    

    Also, containsKey has the additional conditional and method call (note that getEntry will return an Entry object, if said key exists, even if the stored value is null):

    352           return getEntry(key) != null;
    

    I suppose it could be argued that containsKey would benefit – in terms of “performance” – from having a specialized form (at the expense of less-DRY code), or that getEntry could follow the lead of get with an early null-key check .. on the other-hand, it might be argued that get should be written in terms of getEntry 😉

    Happy coding.

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

Sidebar

Related Questions

Consider this HashMap extention (generates an instance of the V class when calling get
Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
Calling index.php?pConta=1&pDataInicial=01-01-2000&pDataFinal=31-12-2000 I get this notices: [08-Oct-2009 17:30:35] PHP Notice: Undefined index: pConta in
Calling TextView.setTextSize() is working abnormally. Right after the call to setTextSize if we get
Calling : $.ajax({ type: POST, url: 'http://blahblah/cow.php', contentType: application/json, data: { 'guid': '111', 'score':
After calling to a com component procedure , an error raised OLE error C0000094.
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
Calling all C macro gurus... Is there any way to write a C macro
Calling TextRenderer.MeasureText as follows: TextRenderer.MeasureText(myControl.Text, myControl.Font); and comparing the result to the size of
Calling image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) will maintain the aspect ratio. But I need

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.