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

The Archive Base Latest Questions

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

I wrote a class that overrides the equals(Object) method in class Object to compare

  • 0

I wrote a class that overrides the equals(Object) method in class Object to compare objects of the class type to other objects of a class type using the object’s instance values.

When I put an instance of the object in a HashMap as the key, and then call get(Object) on the map with a new but identical object as the key, it returns null.

I’ve tried passing a new, identical object to the equals method and it returns true, so the problem isn’t my comparison code.

From what I’ve gathered through debugging, the equals(Object) method in my object is never called.

But if you use a String key in a HashMap and then pass a new instance with identical characters to get(Object), it returns the value successfully.

Why is this happening? What do I have to do to have HashMap test keys based on MY equals method?

  • 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-05T05:58:30+00:00Added an answer on June 5, 2026 at 5:58 am

    You need to also override Object.hashcode(). Take a look at the link, as it specifies that hashcode() and equals() have a contract to ensure proper functionality in HashTable‘s, HashMap‘s, and HashSet‘s.

    In a HashMap, values are stored in buckets, which are reached by the hashcode of the key. Once the proper bucket is found, the equals method is then applied to each member of the bucket until equality is determined. Because of this, it is important to make sure that your hash algorithm ‘hashes well’.

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

Sidebar

Related Questions

So I recently wrote a java class that extends exception and used an instance
My main solution is using code from a utility class library, that I wrote
I need to implement equals() method for an Item that may be put in
I'm working on a PHP class that I wrote. Its setColour() method typehints for
i wrote a simple class that derives QPushButton, and tried to apply stylesheet on
I'm doing an OS class that's based on xv6 and I wrote a program
here is the piece of code that i wrote: public class ServletCounter extends HttpServlet
http://ideone.com/u0bVy the link above contain the code that i wrote for a class project.I
I am using Qt and wish to write a class that will perform some
I have a class Foo which overrides equals() and hashCode() properly. I would like

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.