Implementierung von Hashcode und Equals für benutzerdefinierte Klassen

Ich habe also viele benutzerdefinierte Klassen und auch benutzerdefinierte Klassen, die Komposition verwenden.

Meine benutzerdefinierten Klassen haben Variablen, die sich sehr häufig ändern, und ich füge sie HashSets hinzu. Meine Frage ist also, wann ich hashCode implementiere - was soll ich für eine Klasse tun, die nur private Felder hat, die sich ständig ändern?

Hier ist ein Beispiel für eine benutzerdefinierte Klasse:

public class Cell {
    protected boolean isActive;
    protected boolean wasActive;

    public Cell() {
    this.isActive = false;
    this.wasActive = false;
    }

    // getter and setter methods...

    @Override
    public int hashCode() {
    // HOW SHOULD I IMPLEMENT THIS IF THIS custom object is constantly
        // being added into HashSets and have it's private fields isActive
        // and wasActive constantly changed.
    }

    // ANOTHER QUESTION Am I missing anything with this below equals implementation?
    @Override
    public boolean equals(Object object) {
    boolean result = false;
    if (object instanceof Cell) {
        Cell otherCell = (Cell) object;
        result = (this.isActive == otherCell.isActive && this.wasActive == 
            otherCell.wasActive);
    }
    return result;
    }

Antworten auf die Frage(3)

Ihre Antwort auf die Frage