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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:32:07+00:00 2026-05-17T19:32:07+00:00

This might seem a simple question but I’ve been trying for a couple of

  • 0

This might seem a simple question but I’ve been trying for a couple of hours now (without having to implement hashCode comparison) to make containsKey working. To simplify things I’ll post a simple code example that i’m having issues with:

public class myPair {
private int a;
private int b;

    myPair(int x, int y) {
        a=x;
        b=y;
    }

    public boolean equals(Object pair) {
        System.out.println("Ola");
        return true;
    }   


    int first()  { return a; }
    int second() { return b; }

    public String toString() {
        return "X: "+this.a + " Y:"+this.b; 
}
}

public class Main {
    public static void main(String args[]){
        Map<myPair,String> myMap = new LinkedHashMap<myPair, String>();
        myMap.put(new myPair(2, 2), "encontrou me");
        if(myMap.containsKey(new myPair(2, 2))){
            System.out.println(myMap.get(new myPair(2, 2)));
        }
        System.out.println(myMap.get(new myPair(2,2)));
    }
}

This outputs:

null

I’ve implemented the equals method…why isn’t it working?

  • 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-17T19:32:08+00:00Added an answer on May 17, 2026 at 7:32 pm

    Because you must override hashCode in order to use a HashMap (or LinkedHashMap). That’s just how hash maps work: They first compute the hash code to get a rough idea of where to look for the object. If the hash code does not equal the target objects hash code, it will simply look for the object in the wrong place!

    This is from the API-documentation of Object.hashCode:

    • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

    The default implementation of hashCode will most of the times not return the same hash code for two distinct object.

    Basically, you’re violating the contract of the API by overriding equals but not hashCode.

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

Sidebar

Related Questions

This might seem like a simple question. But I have been looking for an
This might be a simple question, but i can't seem to grasp it. I
This might seem ridiculously simple, but I've been getting all kinds of error depending
This might be a way to simple question, but I seem to miss the
this might be a simple question but i can't seem to figure it out.
This might seem like a simple question but Id like to know the fastest
I know this might seem a simple question, but with all the WMD versions
This might seem like a very simple question, but I am struggling with it.
This question might seem strange but I have been searching for an answer for
This might seem like a very easy question for some of you folks, but

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.