Java generic pairs stored in HashMap cannot correctly retrieve key – > value

This is 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);
  }
}

This is a test case:

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>(); 

    Pair<String,String> p1 = new Pair<String,String>("Test1","Value1"); 
       hm1.put(p1,"ONE");  
    Pair<String,String> p2 = new Pair<String,"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,"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,"Value2"); 
       hm1.put(p6,"SIX");  
    Pair<String,String> p7 = new Pair<String,"Value3"); 
       hm1.put(p7,"SEVEN");    

    assertTrue( hm1.size() == 7 ); 

    Pair<String,String> pSrch = new Pair<String,"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"));

  } 
}

This is my question, the last Hm1 The containskey call should (I naively expect) return the value stored in it < "three", "three" > Yes - I should get a string with the value "seven" This 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 P7 Hashcode() and psrch Hashcode () is equal, P7 Equals (psrch) and psrch Equals (P7), and Hm1 Containsvalue (P7) = = true, I hope Hm1 Containsvalue (psrch)) will also return true, but this is not the case What did I miss?

Solution

You need to start from Java The lang.Object class overrides the equals method

Instead, you have overloaded the method with an additional version of the It will never be called a completely different approach Replace your equality 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 error, use the @ override annotation for the method to override If not, you will receive a compile - time error

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>