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

The Archive Base Latest Questions

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

In Java, having a HashMap fully filled in with data of such form: HashMap<Integer,

  • 0

In Java, having a HashMap fully filled in with data of such form:

HashMap<Integer, int[]> map = new HashMap<Integer, int[]>(1000000, 1);

what is faster when checking the existence of a random key, say 100:

if (map.get(100) == null))

or

if (!map.containsKey(100))

?

Question is interesting from the micro-optimization point of view.

  • 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-18T14:27:58+00:00Added an answer on June 18, 2026 at 2:27 pm

    The containsKey should be very slightly slower because it results in an extra function call (it just calls getEntry) (it could get optimised away, I’m not sure whether Java will do so). containsKey looks like:

    public boolean containsKey(Object key) {
      return getEntry(key) != null;
    }
    

    But note that containsKey could on the other hand be very slightly faster on other Map implementations (but probably not those in the standard Java API).

    Generally my implementations look like: (avoiding the need for containsKey)

    int[] arr = map.get(100);
    if (arr == null) // doesn't exist
      // do stuff
    else // exists
      // do stuff with arr
    

    The below would definitely be slower than the above: (if the items you look for exist a reasonable amount of the time)

    if (!map.containsKey(100)) // doesn't exist
      // do stuff
    else // exists
    {
      int[] arr = map.get(100);
      // do stuff with arr
    }
    

    Edit: Thanks to zvzdhk for providing the source of containsKey. I should actually have checked.

    • 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 get all rows data from sql. DatabaseHandler.java public HashMap<String,
I'm new to regex in Java and am having trouble getting it to work
I have often heard complaints against Java for not having unsigned data types. See
I am having query which return java.util.HashMap. It return hashmap when query return with
I am having OOM issues with java.util HashMap so I am looking for memory
I am building a Text Editor in Java(Swing) having an EditorPane to type the
I am having problems having my Java programs see classes that are packaged in
Using Linux environment with java,I'm having the config file which should be configured before
I just started learning C++ (coming from Java ) and am having some serious
I want to be able to support multiple versions of Java ME without having

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.