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

  • Home
  • SEARCH
  • 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 6171673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:15:30+00:00 2026-05-23T23:15:30+00:00

I want to compare two Java Map s by a simple hash. Each object

  • 0

I want to compare two Java Maps by a simple hash.

Each object is on a different computer, so sending a hash over the network will be cheaper that sending the whole object to compare.

For example I have two HashMaps of an ExampleClass

Map<String,ExampleClass> One=new ...;

Map<String,ExampleClass> Other=new ...;

I don’t need to be sure that all elements are equal,
it’s enough for me to trust in a hash.

I was about to iterate at each side and create a “homemade Hash”, then send it to the network to finally compare for example an int or something.

It would be great if this “hash” is calculated every time an object is added or deleted from the Collection, saving me from iterate the whole object. I have to encapsulate every add/delete of the Map. Is there a Java library that does this?

  • 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-23T23:15:30+00:00Added an answer on May 23, 2026 at 11:15 pm

    If all your classes implement hashCode() (does not use the “default” memory address hashcode) you can use the map’s hashCode().

    The caveat here is that if your ExampleClass does not implement hashCode(), then equal items might have different hashes on the two different machines, which will result in different hashes for the maps.


    To clarify:

    Map implements a hashCode() that is defined as the sum of it’s Map.Enytry‘s hashCode()s.

    Map.Entry‘s hashCode() is defined to be the xor of the key’s hashCode() and the value’s hashCode().
    Your keys are Strings — they have a well defined hashCode() (two equal strings always have the same hashCode()).
    Your values are ExampleClass instances — they also need a well-defined hashCode().

    In summary, a map that contains { s1 -> ec1, s2 -> ec2 } will have a hashcode equal to:

    (s1.hashCode() ^ ec1.hashCode()) + (s2.hashCode() ^ ec2.hashCode())
    

    meaning that it depends on ExampleClass‘s hashCode().

    If ExampleClass did implement hashCode() in a way that equal ExampleClasses give equal hashCode()s, everything will work well.
    If ExampleClass did not implement hashCode(), it will use Object‘s hashCode(), which will almost always give you different hashCodes().

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

Sidebar

Related Questions

I need to compare two different files of the instance File in Java and
What's the best way to compare two JSON strings in Java? I want to
I want to compare two Java objects without overriding equals method . Since I
I want to compare two lists in java and build two lists oneFound and
I want to compare the date part of two java.util.Date objects. How can I
I want to compare two ms-access .mdb files to check that the data they
can i somehow compare two numbers in regex? i want regex that is correct
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I want to write a function that accepts two objects as parameters and compare
I want to compare two values at runtime using reflection. I was using Comparer.Default.Compare(x,y)

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.