2016-07-23 2 views
0

ハッシュマップからいくつかの値を取得しようとしていますが、キーがマップ内に存在するかどうかを確認しています。このチェックは常に失敗し、null値になります。私はオーバーライドされたハッシュコードと同様のメソッドを持っています。誰かが私がここで間違っていることを教えてもらえますか?私のHashMap.getは、適切なキー、値を入れた後でもnull値を返します。

クラスフィールド:私は値を取得しています

public static synchronized void handleSubs(String[] subData,String dz){ 
    int[] lowdims=new int[subData.length]; 
    int[] highdims=new int[subData.length]; 
    try { 
     for (int i=1;i<subData.length;i++){ 
      if (!subData[i].equals("") && !subData[i].equals("\n")){ 
       if (i%2==0){ 
        highdims[i]=Integer.parseInt(subData[i].trim()); 
       } 
       else { 
        lowdims[i]=Integer.parseInt(subData[i].trim()); 
       } 
      } 
     } 
     if (!DZ.isEmpty()){ 
      DZ.clear(); 
     } 
     DZ.add(dz); 
     allSubDZs.add(dz); 
     int[] newlow=removeZeroes(lowdims); 
     int[] newhigh=removeZeroes(highdims); 
     allSubs.add(new Participant(newlow,newhigh)); 
     subDz.put(new Participant(newlow,newhigh),DZ); 
    } 

方法:私はマップに入れています

private static final List<String> DZ=new ArrayList<String>(); 
private static final Map<Participant,List<String>> subDz=new HashMap<Participant,List<String>>(); 

方法

public static List<String> getSubDz(Participant sub){ 
    if (subDz.containsKey(sub)){ 
     return subDz.get(sub); 
    } 
    else{ 
     logger.info("Subscription DZ not available"); 
     return null; 
    } 
} 

ザ・場合にチェックgetSubDzはキーを入れても常に失敗します。

のhashCodeとequalsメソッドは:

@Override 
    public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((DZ == null) ? 0 : DZ.hashCode()); 
    return result; 
} 
    @Override 
    public boolean equals(final Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (this.getClass() != obj.getClass()) { 
     return false; 
    } 
    final SubscriptionHandler other=(SubscriptionHandler)obj; 
    if (DZ == null) { 
     if (other.DZ != null) { 
      return false; 
     } 
    } else if (!DZ.equals(other.DZ)) { 
     return false; 
    } 
    return true; 
+0

なぜすべて静的ですか? –

+0

いくつかのアイテムを追加した後にハッシュマップ全体を印刷するとどうなりますか? –

+1

'Equals'と' hashCode'は 'static'フィールドだけを読み込んでいますが、意味がありません。 また、Participantの 'equals'と' hashCode'はどのように実装されていますか?あなたは同じ 'Participant'または等しい' Participant'で 'getSubDz'を呼び出していますか? – garnulf

答えて

1

あなたはキークラス上のequalsとhashCodeが必要です。これは、あなたの場合の参加者クラスになります。

関連する問題