2016-04-13 1 views
0

動作しないために:カスタムキーハッシュマップは、私は私のハッシュマップのカスタムキークラスを作った

package com.amit.test; 

public class MyCustomKey { 
    int customerId; 

    public MyCustomKey(int customerId){ 
     this.customerId = customerId; 
    } 

    public int getCustomerId() { 
     return customerId; 
    } 

    public void setCustomerId(int customerId) { 
     this.customerId = customerId; 
    } 

    public int hashCode(){ 
     //return Double.valueOf(Math.random()).intValue() * customerId; 
     return customerId; 
    } 

    public boolean equals(MyCustomKey customKey){ 
     if(this == customKey) 
      return true; 
     if(this.getCustomerId() == customKey.getCustomerId()) 
      return true; 
     return false; 
    } 
} 
次のように私は、メインクラスで使用

package com.amit.test; 

import java.util.HashMap; 
import java.util.Map; 
import java.util.Set; 

public class MyCustomHashMap { 

    public static void main(String[] args) { 
     Map<MyCustomKey, String> map = new HashMap<MyCustomKey, String>(); 
     MyCustomKey customKey1 = new MyCustomKey(1); 
     System.out.println("Custom Key 1 hashCode : " + customKey1.hashCode()); 
     MyCustomKey customKey2 = new MyCustomKey(1); 
     System.out.println("Custom Key 2 hashCode : " + customKey2.hashCode()); 
     System.out.println("Custom Key 1 equals Key 2 : " + customKey1.equals(customKey2)); 
     System.out.println("Custom Key 1 == Key 2 : " + (customKey1 == customKey2)); 

     map.put(customKey1, "One"); 
     map.put(customKey2, "Two"); 

     Set<MyCustomKey> keys = map.keySet(); 
     for(MyCustomKey key : keys){ 
      System.out.println(map.get(key)); 
     } 
    } 

} 

出力: :
私のように出力を得る理由 が、私は疑問に思って:

Custom Key 1 hashCode : 1 
Custom Key 2 hashCode : 1 
Custom Key 1 equals Key 2 : true 
Custom Key 1 == Key 2 : false 
One 
Two 

及びません:

Custom Key 1 hashCode : 1 
Custom Key 2 hashCode : 1 
Custom Key 1 equals Key 2 : true 
Custom Key 1 == Key 2 : false 
One 

私のキーオブジェクトには同じint値が含まれているため、私が書いたequalsメソッドと同じように私はtrueを返すので、なぜ 'One' 'Two'と一緒に。私は

@Override 
public boolean equals(Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    final MyCustomKey other = (MyCustomKey) obj; 
    if (this.customerId != other.customerId) { 
     return false; 
    } 
    return true; 
} 

と等しくを変更した場合

+1

私はequalsメソッドを適切にオーバーライドしていないと思います。これはオブジェクトとしてパラメータを受け入れる必要があります、そして、それは多かれ少なかれpublic boolean equals(Object customKey){ \t \t if(this == customKey) \t \t \tのようになります。 \t \t if(this.getCustomerId()==((MyCustomKey)customKey).getCustomerId()) \t \t \t trueを返します。 \t \t falseを返します。 \t} –

+0

メソッドをオーバーライドするたびにアノテーション@Overrideを使用することをお勧めします。 (例:hasCode、およびequalsのメソッド)メソッドのシグネチャがスーパークラスのシグネチャと一致しない場合、コンパイラはエラーをスローします。 – cyroxis

答えて

0

出力は、元のコードで

Custom Key 1 hashCode : 1 
Custom Key 2 hashCode : 1 
Custom Key 1 equals Key 2 : true 
Custom Key 1 == Key 2 : false 
Two 

なった、ハッシュマップは、デフォルトを使用してオブジェクトのクラスによって提供される実装に等しいました。これは、オブジェクトのアドレスを使用します。オブジェクトのアドレスは等しくなく、2つのオブジェクトがマップに含まれます。

関連する問題