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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:18:07+00:00 2026-05-16T16:18:07+00:00

The following code results in the same hash code being generated for the two

  • 0

The following code results in the same hash code being generated for the two maps, any ideas?


import java.util.HashMap;
import java.util.Map;

public class Foo
{
    @SuppressWarnings("unchecked")
    public static void main (String[] args)
    {
        Map map;

        map = new HashMap();

        map.put("campaignId", 4770L);
        map.put("location", "MINI_PROFILE");
        map.put("active", "true");
        map.put("lazy", true);

        System.out.println(map.hashCode());

        map = new HashMap();

        map.put("campaignId", 4936L);
        map.put("location", "MINI_PROFILE");
        map.put("active", "true");
        map.put("lazy", false);

        System.out.println(map.hashCode());


    }
}

The result is:

-1376467648
-1376467648

Simply changing the key names is enough to make the code generate two different hash codes.

  • 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-16T16:18:08+00:00Added an answer on May 16, 2026 at 4:18 pm

    Simply coincidence, I suspect… there are bound to be collisions, and in this case it looks like the relevant different bits in the first value are being lost, effectively.

    However, it shouldn’t make any difference – anything using hash codes must cope with collisions.

    EDIT: It’s just the way the hashes happen to be calculated. This code shows what’s going on:

    import java.util.*;
    
    public class Test
    {
        @SuppressWarnings("unchecked")
        public static void main (String[] args)
        {
            AbstractMap.SimpleEntry[] entries = {
                new AbstractMap.SimpleEntry("campaignId", 4770L),
                new AbstractMap.SimpleEntry("campaignId", 4936L),
                new AbstractMap.SimpleEntry("lazy", true),
                new AbstractMap.SimpleEntry("lazy", false)
            };
            for (AbstractMap.SimpleEntry entry : entries) {
                System.out.println(entry + ": " + entry.hashCode());
            }
        }
    }
    

    Results:

    campaignId=4770: -1318251287
    campaignId=4936: -1318251261
    lazy=true: 3315643
    lazy=false: 3315617
    

    So in one pair the first map has a hash 26 less than the second map, and in another pair the first map has a hash 26 more than the second map.

    AbstractMap just sums hash values (one way of making sure that ordering is irrelevant) so the two end up with the same hash code.

    It’s really down to Boolean.hashCode() which looks like this:

    return value ? 1231 : 1237;
    

    … and Long.hashCode() which looks like this:

    return (int)(value ^ (value >>> 32));
    

    Given the values they happened to pick in Boolean.hashCode(), if your long values are only 26 apart (or 26 * 2^32 apart) then you’ll run into the same thing.

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

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I removed the margin and padding from all elements to… May 17, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer Wallclock seconds is the actual elapsed time, as if you… May 17, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer It appears Doctrine 1 does not support this out of… May 17, 2026 at 1:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Today I tried the following snippets of code and I don't understand why I
When i write the following code Post.find(:all,:group=>'comments') It only gives me the first record
I need a PHP version of the following C# code: string dateSince = 2010-02-01;
I tried to run the following code on the php console of a development
Is it possible for code that meets the following conditions to produce different outputs
I'm still fairly new to working with java and the google appengine datastore. I
I have a table that returns results from a search. When the user search
Alternative wording : When will adding Double.MIN_VALUE to a double in Java not result
I'm having some trouble looping over a HashMap to print out it's values to
Not a real question because I already found out the answer, but still interesting

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.