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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:11:11+00:00 2026-06-14T18:11:11+00:00

I was messing around with some hashCode + equals + Map stuff and found

  • 0

I was messing around with some hashCode + equals + Map stuff and found something … weird.

The snippet is as follows:

class Obj {
    String n;
    Obj(String n) {this.n = n;}

    public int hashCode() {return 0;}
    public boolean equals(Object o) {return false;} // no instance of this class 
                                                    // equals any other instance

}

Then I did something like this:

    java.util.Map<Obj,String> map = new java.util.HashMap<Obj,String>();
    Obj o1 = new Obj("1");
    Obj o11 = new Obj("1");
    Obj o2 = new Obj("2");

    map.put(o1,"val 1");
    map.put(o11,"val 2");
    map.put(o2,"val 3");

    p("size = " + map.size()); // obviously 3
    p(map.get(new Obj("1"))); // obviously null     
    p(map.get(o1)); // ...

The last line is the weird part. The last line returns val 1. How come? The equals method always returns false. Is this because the == operator is used before equals is called?

Thanks for any insight.

  • 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-14T18:11:12+00:00Added an answer on June 14, 2026 at 6:11 pm

    In HashMap.java, the get method is:

    public V get(Object key) {
        if (key == null)
            return getForNullKey();
        int hash = hash(key.hashCode());
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                return e.value;
        }
        return null;
    }
    

    The line if (e.hash == hash && ((k = e.key) == key || key.equals(k))) does indeed compare the keys using == before calling equals. That is why your attempt to eliminate equality fails.

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

Sidebar

Related Questions

I am currently messing around with some stuff for an idea for a site
I'm messing around in Ruby some more. I have a file containing a class
I have been messing around with web.py lately and wanted to grab some stuff
I've been messing around with HTML5 / Javascript and found something I don't understand.
I'm messing around with some JSON and I'm trying to use the JavascriptSeralizer Class
Messing around with some CSS trying to learn some stuff as I haven't done
I'm messing around with some jQuery stuff for a mockup and noticed some odd
I'm messing around with some audio stuff and the algorithm I'm trying to implement
I started messing around with FXCop against some of my assemblies and noticed something
Was messing around with some array stuff earlier and discovered a very peculiar caveat

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.