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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:29:18+00:00 2026-06-11T11:29:18+00:00

I have a problem when retrieving values from a hashmap. The hashmap is declared

  • 0

I have a problem when retrieving values from a hashmap. The hashmap is declared as follows:

HashMap<TRpair,A> aTable = new HashMap<TRpair,A>();

I then put 112 values into the map as follows:

aTable.put(new TRpair(new T(<value>),new Integer(<value>)),new Ai());

where Ai is any one of 4 subclasses that extend A.

I then proceed to check what values are in the map, as follows:

int i = 0;
for (Map.Entry<TRpair,A> entry : aTable.entrySet()) {
    System.out.println(entry.getKey().toString() + " " + entry.getValue().toString());
    System.out.println(entry.getKey().equals(new TRpair(new T("!"),new Integer(10))));
    i++;
}

i holds the value 112 at the end, as one would expect and the equality test prints true for exactly one entry, as expected.

However, when I do

System.out.println(aTable.get(new TRpair(new T("!"), new Integer(10))));

null is output, despite the above code snippet confirming that there is indeed one entry in the map with exactly this key.

If it helps, the class TRpair is declared as follows:

public class TRpair {
    private final T t;
    private final Integer r;

    protected TRpair(Integer r1, T t1) {
        terminal = t1;
        row = r1;
    }

    protected TRpair(T t1, Integer r1) {
        t = t1;
        r = r1;
    }

    @Override
    public boolean equals(Object o) {
        TRpair p = (TRpair)o;
        return (p.t.equals(t)) && (p.r.equals(r));
    }

    @Override
    public String toString() {
        StringBuilder sbldr = new StringBuilder();
        sbldr.append("(");
        sbldr.append(t.toString());
        sbldr.append(",");
        sbldr.append(r.toString());
        sbldr.append(")");
        return sbldr.toString();
    }
}

the equals() and toString() methods in each of the Ai (extending A) and in the T class are overridden similarly and appear to behave as expected.

Why is the value output from the hashmap aTable null, when previously it has been confirmed that the value for the corresponding key is indeed in the map?

With many thanks,

Froskoy.

  • 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-11T11:29:19+00:00Added an answer on June 11, 2026 at 11:29 am

    The keys/elements for a Hash collection but override hashCode() if euqals is overridden.

    You could use.

    public int hashCode() {
        return t.hashCode() * 31 ^ r.hashCode();
    }
    

    BTW: It appears from your code that Integer r cannot be null in which case using int r makes more sense.

    From Object.equals()

    Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

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

Sidebar

Related Questions

in my app I have no problem withs saving/retrieving string values(like myTextField.text ) into
I have a problem retrieving default values from the settings bundle when first launching
i have a problem when it comes to retrieving value from jQuery to php.i
I have this strange problem. I am retrieving twitters and it works on the
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
I am retrieving some data from a table in mysql. The values are stored
I have small problem with retrieving the state of a canvas' visibility property. When
I have a problem on arraylist and hashmap As according to my requirement, I
I am retrieving a list of values from a sharepoint list, which works well

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.