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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:30:58+00:00 2026-06-05T21:30:58+00:00

While perusing the source for AbstractMap in Java I ran across this: 440 /**

  • 0

While perusing the source for AbstractMap in Java I ran across this:

440      /** 
441       * Returns the hash code value for this map.  The hash code of a map is 
442       * defined to be the sum of the hash codes of each entry in the map's 
443       * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt> 
444       * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps 
445       * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of 
446       * {@link Object#hashCode}. 
447       * 
448       * <p>This implementation iterates over <tt>entrySet()</tt>, calling 
449       * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the 
450       * set, and adding up the results. 
451       * 
452       * @return the hash code value for this map 
....  
456       */ 
457      public int hashCode() { 
458      int h = 0; 
459      Iterator<Entry<K,V>> i = entrySet().iterator(); 
460      while (i.hasNext()) 
461          h += i.next().hashCode(); 
462      return h; 
463      }

This is interesting because of what it – seemingly accidentally – precludes in the way of hashcodes.

If this method is to work as written, it precludes the use of hashcodes which, when summed together, exceed Integer.MAXINT. If you’re writing your own hashcode, you may want to know about this.

I can think of at least one useful definition of hashcode that could run afoul of this and moreover it seems to be sensitive to the amount of data in the hashmap. Specifically, the more data in the map, the larger the entrySet, the larger the running total of hashcodes.

This really seems like an undocumented side-effect and also just a plain old Bad Idea. The intent seems to be to leverage the commutative law of addition (a+b == b+a) to produce the required equality of maps with the same entries, but wow, what a bad implementation.

This requires anyone overriding hashcode – which is anyone who doesn’t want merely object-instance brand equality ( == i.e. most people), to know things which they can’t or aren’t likely to know. The first one is the cumulative sum of their hashcodes (who ever thinks of this??) and the other is the maximum number of items that will ever be entered into a map and how that could effect the cumulative sum.

This is just whacked-a-doodled. Anyone have any insight? hashcode() is derived from the class Object if it matters.

  • 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-05T21:31:00+00:00Added an answer on June 5, 2026 at 9:31 pm

    int will wrap around / overflow so going above Integer.MAX_VALUE will not throw an exception or cause problems here.

    The main concept here is that hashCode should yield identical (integer) values for objects that are considered identical (in a particular run of your program). This code fulfills that requirement.

    Note that hashcode collisions can and will happen occasionaly, that’s you always need to provide a meaningful equals override when overriding hashcode.

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

Sidebar

Related Questions

While browsing the source code of the Scala API, I met this package definition
While listening through Stanford's Programming Abstractions course, I come across some piece of code
While executing this below Dojo code the call back mehod is calling the onFailure.
While looking at various options for making dropdowns more user-friendly I came across this
While using boost::threads I have come across this interruption problem. When I do a
Several times, while perusing the Boost library's documentation, I've run across return values that
While debugging and keep pressing F5, if the source code does not exist, eclipse
While studying an implementation of Group signature I came across code where Group certificate
While I am trying to debug this code (in C# WinForms), it shows an
while($c=fgets(STDIN)){ if($c===PHP_EOL){ continue; } else { echo $c; } } When the above code

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.