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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:59:58+00:00 2026-06-01T06:59:58+00:00

Some major JVM classes (such as String or List implementations ) implement equals by

  • 0

Some major JVM classes (such as String or List implementations) implement equals by returning Σ 31^n * field_n.hashCode() for every field_n that is relevant for the equals method. Beside, this approach is recommended by Joshua Bloch in Effective Java (item 9).

However, other classes such as Map.Entry implementations follow different rules. For example, the Map.Entry documentation states that the hash code of a Map.Entry should be

 (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
 (e.getValue()==null ? 0 : e.getValue().hashCode())

This can sometimes be impractical to use in hash tables, since:

  • the hash code of all entries which have the same key and value is 0,
  • two entries e1 and e2 so that e1.key = e2.value and e1.value = e2.key have the same hash code.

Why did Java choose this implementation specification for Map.Entry hashCode instead of, for example, 31 * (e.getKey()==null ? 0 : e.getKey().hashCode()) + (e.getValue()==null ? 0 : e.getValue().hashCode())?

Edit 1:

To help figure out the problem, here is an example of useful code where the result has very poor performance due to hash collisions if many entries have the same key and value.

This method computes the frequencies of the entries of different maps (using Guava’s Multiset).

public static <K, V> Multiset<Map.Entry<K, V>> computeEntryCounts(
        Iterable<Map<K, V>> maps) {
    ImmutableMultiset.Builder<Map.Entry<K, V>> result = ImmutableMultiset.builder();
    for (Map<K, V> map : maps) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            result.add(entry);
        }
    }
    return result.build();
}
  • 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-01T06:59:59+00:00Added an answer on June 1, 2026 at 6:59 am

    I doubt there’s a good reason — I think it’s just an oversight — but it’s not a serious problem. It would only come up if you had a HashSet<Map.Entry<T,T>> or a HashMap<Map.Entry<T,T>,V>, which is not commonly done. (Edited to add: Or, as Joachim Sauer points out below, a HashSet<Map<T,T>> or a HashMap<Map<T,T>,V> — also not commonly done.)

    Note that HashMap<K,V> does not use Map.Entry<K,V>.hashCode(), because it looks up entries by their keys only, so it just uses K.hashCode().

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

Sidebar

Related Questions

Let's say you recently discovered some major vulnerabilities in a couple of websites that
My colleague mentioned that there are some major improvements in CLR 4.0 related to
Right now I have some major code changes that won't be done in time
I'm having some major problems trying to get a ? inside of hrefs that
I've had a major annoyance with the C++ Builder IDE for some time now.
Some fancy websites show an error dialog when it is detected that an untrained
Some APIs only support output to files. e.g. a library that converts a BMP
Since there are some major privacy issues with alot of social networking sites I
Last night one of our SQL servers developed some major problems and after a
I'd like to mimic a .NET class signature (and possibly some major pieces of

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.