2015-03-30 88 views
2

我創建了一個應用程序,並且我爲ObservableList創建了模型我想調用集合.contains()這個方法需要equals(),但這總是返回false。我只在equals方法中放入一個字段。當我在if語句中比較使用getter方法時,它會像我期望的那樣返回true。JavaFx模型等於總是返回false

import java.util.Objects; 
import javafx.beans.InvalidationListener; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.image.Image; 

public class Client implements ObservableValue { 

    private final SimpleStringProperty remoteAddress; 
    private final SimpleStringProperty userName; 
    private final boolean connected = true; 
    private final ObservableList<String> logs 
      = FXCollections.observableArrayList(); 
    private final ObservableList<Image> imageLogs 
      = FXCollections.observableArrayList(); 

    public Client(String remoteAddress, String userName) { 
     this.remoteAddress = new SimpleStringProperty(remoteAddress); 
     this.userName = new SimpleStringProperty(userName); 
    } 

    public SimpleStringProperty remoteAddressProperty() { 
     return remoteAddress; 
    } 

    public SimpleStringProperty userNameProperty() { 
     return userName; 
    } 

    public final String getRemoteAddress() { 
     return remoteAddress.get(); 
    } 

    public final void setRemoteAddress(String name) { 
     this.remoteAddress.set(name); 
    } 

    public final String getUserName() { 
     return userName.get(); 
    } 

    public final void setUserName(String name) { 
     this.userName.set(name); 
    } 

    public ObservableList<String> getLogs() { 
     return logs; 
    } 

    public ObservableList<Image> getImageLogs() { 
     return imageLogs; 
    } 

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

    @Override 
    public int hashCode() { 
     int hash = 3; 
     hash = 37 * hash + Objects.hashCode(this.remoteAddress); 
     return hash; 
    } 
} 

回答

2

你需要的屬性的實際字符串內容比較:

@Override 
public boolean equals(Object obj) { 
    if (obj == null || getClass() != obj.getClass()) { 
     return false; 
    } 
    return this.remoteAddress.get().equals(((Client) obj).remoteAddress.get()); 
} 

@Override 
public int hashCode() { 
    int hash = 3; 
    hash = 37 * hash + Objects.hashCode(this.remoteAddress.get()); 
    return hash; 
} 
+0

但爲什麼我可以從'TableView'中刪除數據?我最近通過調用'table.getItems()。remove(row.getItem())'從tableview(包含Person類對象)中刪除數據,即使我沒有重寫equals和hashCode方法。 – UnKnown 2016-11-04 16:00:20

+0

@未知我真的不知道這與這個問題有什麼關係。可能您需要發佈自己的問題,爲您的代碼提供適當的上下文。 – 2016-11-04 16:06:52

+0

我在這裏發佈了新的問題。 http://stackoverflow.com/questions/40427758/how-tableview-and-listviews-remove-works – UnKnown 2016-11-04 16:42:21

-1

我認爲,你不能比較兩個對象 「==」,使用等於:

return this.remoteAddress.equals(((Client) obj).remoteAddress); 
+0

同樣,假設'SimpleStringProperty'重寫'equals(...)'。它沒有。 – 2015-03-30 18:26:51

+0

@James_D我刪除了我的答案(與此相同),因爲即使字符串「equal」,equals也返回false。然而根據文檔'equals' *被重載。根據文檔'SimpleStringProperty'從'ReadOnlyStringProperty'繼承'equals',而不是'Object'。 – 2015-03-30 18:53:22

+0

我看到的[Javadoc中的'equals(...)'](http://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/SimpleStringProperty.html)是唯一的地方它是從'Object'繼承的。同樣,在['ReadOnlyStringProperty'](http://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/ReadOnlyStringProperty.html)我沒有看到'equals(...)'方法。 – 2015-03-30 19:01:02