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

  • Home
  • SEARCH
  • 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 179335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:23:20+00:00 2026-05-11T14:23:20+00:00

Here’s Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB>

  • 0

Here’s Pair.java

import java.lang.*;  import java.util.*;   public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB> > {   protected final TYPEA Key_;   protected final TYPEB Value_;    public Pair(TYPEA key, TYPEB value) {     Key_   = key;     Value_ = value;   }   public TYPEA getKey() {     return Key_;   }   public TYPEB getValue() {     return Value_;   }   public String toString() {     System.out.println('in toString()');     StringBuffer buff = new StringBuffer();       buff.append('Key: ');       buff.append(Key_);       buff.append('\tValue: ');       buff.append(Value_);     return(buff.toString() );   }   public int compareTo( Pair<TYPEA, TYPEB> p1 ) {      System.out.println('in compareTo()');     if ( null != p1 ) {        if ( p1.equals(this) ) {          return 0;        } else if ( p1.hashCode() > this.hashCode() ) {              return 1;       } else if ( p1.hashCode() < this.hashCode() ) {          return -1;         }     }     return(-1);   }   public boolean equals( Pair<TYPEA, TYPEB> p1 ) {      System.out.println('in equals()');     if ( null != p1 ) {        if ( p1.Key_.equals( this.Key_ ) && p1.Value_.equals( this.Value_ ) ) {          return(true);       }     }     return(false);   }   public int hashCode() {      int hashCode = Key_.hashCode() + (31 * Value_.hashCode());     System.out.println('in hashCode() [' + Integer.toString(hashCode) + ']');     return(hashCode);   } } 

Here’s the testcase:

import java.lang.*;  import java.util.*;  import junit.framework.*;  public class PairTest extends TestCase {     public void testPair() {      String key   = new String('key');      String value = new String('asdf');       Pair<String, String> pair = new Pair<String, String>( key, value );       assertTrue( pair.getKey().equals( key ) );     assertTrue( pair.getValue().equals( value ) );     assertTrue( pair.equals( new Pair<String, String>(key, value)) );   }    public void testPairCollection() {       HashMap< Pair<String, String>, String> hm1 = new HashMap<Pair<String,String>, String>();       Pair<String, String> p1 = new Pair<String, String>('Test1', 'Value1');         hm1.put(p1, 'ONE');       Pair<String, String> p2 = new Pair<String, String>('Test1', 'Value2');         hm1.put(p2, 'TWO');       Pair<String, String> p3 = new Pair<String, String>('Test2', 'Value1');         hm1.put(p3, 'THREE');         Pair<String, String> p4 = new Pair<String, String>('Test2', 'Value2');         hm1.put(p4, 'FOUR');      Pair<String, String> p5 = new Pair<String, String>('Test3', 'Value1');         hm1.put(p5, 'FIVE');      Pair<String, String> p6 = new Pair<String, String>('Test3', 'Value2');         hm1.put(p6, 'SIX');       Pair<String, String> p7 = new Pair<String, String>('Test3', 'Value3');         hm1.put(p7, 'SEVEN');          assertTrue( hm1.size() == 7 );       Pair<String, String> pSrch = new Pair<String, String>('Test3', 'Value3');      assertTrue( p7.equals(pSrch) );     assertTrue( pSrch.equals(p7) );     assertTrue( p7.hashCode() == pSrch.hashCode() );      assertTrue( 0 == p7.compareTo( pSrch ) );     assertTrue( 0 == pSrch.compareTo(p7) );      System.out.println('starting containsKey search');     assertTrue( hm1.containsKey( p7 ) );     System.out.println('starting containsKey search2');     assertTrue( hm1.containsKey( pSrch ) );     System.out.println('finishing containsKey search');      String result = hm1.get( pSrch );     assertTrue( null != result );     assertTrue( 0 == result.compareTo('SEVEN'));    }  } 

Here’s my problem, the last hm1.containsKey call should (I naively expect) return the value stored where Pair<‘Three’, ‘Three’> is true – I should get a String with a value of ‘SEVEN’. Here is the output:

Running in equals() in hashCode() [1976956095] in hashCode() [1976956126] in hashCode() [1976956096] in hashCode() [1976956127] in hashCode() [1976956097] in hashCode() [1976956128] in hashCode() [1976956159] in equals() in equals() in hashCode() [1976956159] in hashCode() [1976956159] in compareTo() in equals() in compareTo() in equals() starting containsKey search in hashCode() [1976956159] starting containsKey search2 in hashCode() [1976956159]     <--- Bug here?  Never reaches            String result = hm1.get( pSrch ); 

So is both p7.hashCode() and pSrch.hashCode() are equal and p7.equals(pSrch) and pSrch.equals(p7), and hm1.containsValue(p7) == true, I would expect hm1.containsValue(pSrch) would also return true, but it does not. What am I missing?

  • 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. 2026-05-11T14:23:21+00:00Added an answer on May 11, 2026 at 2:23 pm

    You need to override the equals method from the java.lang.Object class.

    Instead, you’ve overloaded the method with an additional version that takes a Pair. Totally different method that never gets called. Replace your equals with something like this:

    @Override public boolean equals(Object o) {    System.out.println('in equals()');   if (o instanceof Pair) {      Pair<?, ?> p1 = (Pair<?, ?>) o;     if ( p1.Key_.equals( this.Key_ ) && p1.Value_.equals( this.Value_ ) ) {        return(true);     }   }   return(false); } 

    To avoid this kind of mistake, use the @Override annotation on methods you intend to act as overrides. You’ll get a compile time error when they don’t.

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

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • 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 .Net is a platform, not a language. If you choose… May 12, 2026 at 6:38 pm
  • Editorial Team
    Editorial Team added an answer To Debug a C++ program alot of extra information has… May 12, 2026 at 6:38 pm
  • Editorial Team
    Editorial Team added an answer Well, put simply, you can't. A TSV is a plain… May 12, 2026 at 6:38 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

Trending Tags

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

Top Members

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.