2015-11-19 37 views
0

我想知道如何檢查重複到二維地圖。 的問題是關於這個樣本代碼Java檢查重複到二維地圖

 Pair<Integer, String> pair1 = new Pair<Integer, String>(); 
     pair1.First = 1; 
     pair1.Second = "A"; 

     Pair<Integer, String> pair2 = new Pair<Integer, String>(); 
     pair2.First = 1; 
     pair2.Second = "A"; 

     Map<Pair<Integer, String>, Double> map 
           = new HashMap<Pair<Integer,String>, Double>(); 
     map.put(pair1, 0.0); 

     System.out.println(map.keySet().contains(pair2)); 
     System.out.println(map.containsKey(pair2)); 
     System.out.println(map.get(pair2)!=null); 

爲什麼是輸出:

false 
false 
false 

? 如何檢查重複項? 在此先感謝

+1

你能張貼代碼爲您Pair類?我懷疑它不是覆蓋equals和hashcode() – whaleberg

+0

import com.sun.star.beans.Pair; – Franky

+0

Hmn。我似乎無法找到它的來源。你可以查看它,看看它是否覆蓋equals()和hashCode? – whaleberg

回答

0

我懷疑你的配對班級沒有正確定義equals()hashCode()

如果Pair的定義是這樣的:

public class Pair<T, U>{ 
    T First; 
    U Second; 
} 

然後你就會看到你獲得的結果。默認情況下,java使用對象標識作爲相等比較,所以即使它們具有相同的內容,兩個不同的對也是不相等的。您可以覆蓋equalshashCode以提供更有意義的比較。

public class Pair<T, U>{ 
    T First; 
    U Second; 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) { 
      return true; 
     } 
     if (o == null || getClass() != o.getClass()) { 
      return false; 
     } 

     Pair<?, ?> pair = (Pair<?, ?>) o; 

     if (First != null ? !First.equals(pair.First) : pair.First != null) { 
      return false; 
     } 
     return !(Second != null ? !Second.equals(pair.Second) : pair.Second != null); 

    } 

    @Override 
    public int hashCode() { 
     int result = First != null ? First.hashCode() : 0; 
     result = 31 * result + (Second != null ? Second.hashCode() : 0); 
     return result; 
    } 
} 

與您的代碼使用時,這將產生:

true 
true 
true 
0

因爲你暗中使用的方法pair1.equals(pair2)比較java.lang.Object類。它的定義:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).Java 8 API

由於您Pair類沒有重載.hashCode().equals(Object other)方法=>比較x == y回報false

這會工作:

public class NewClass { 
static class Pair<I,S>{ 
    private I First; 
    private S Second; 

    @Override 
    public int hashCode(){ 
     return First.hashCode() + 23*Second.hashCode(); 
    } 

    @Override 
    public boolean equals(Object other){ 
     Pair<I, S> otherPair = (Pair<I, S>) other; 
     return (this == null ? otherPair == null : (this.First == otherPair.First && this.Second == otherPair.Second)); 
    } 
} 
public static void main(String[] args) { 
    Pair<Integer, String> pair1 = new Pair<Integer, String>(); 
    pair1.First = 1; 
    pair1.Second = "A"; 

    Pair<Integer, String> pair2 = new Pair<Integer, String>(); 
    pair2.First = 1; 
    pair2.Second = "A"; 

    Map<Pair<Integer, String>, Double> map = new HashMap<Pair<Integer,String>, Double>(); 
    map.put(pair1, 0.0); 

    System.out.println(pair1.equals(pair2)); 
    System.out.println(map.containsKey(pair2)); 
} 
}