Éowyn
Éowyn

Reputation: 187

Java HashSet is allowing duplicates

I need to create an ID based on a 15x15 matrix values and since it is not possible to create an integer of size 15, I tried the following reasoning to create an ID of type double:

First I create a String with the values of the cells and while I do this, I look for the cell that has a value of 0. When I find I enter a dot "." in the String. Then I convert my String to BigDecilmal and the method I call doubleValue ().

   public double generateId() {
        String sid = "";
        for (int i = 0; i < this.matrix[0].length; i++) {
            for (int j = 0; j < matrix[1].length; j++) {
                if (matrix[i][j].equals("0")) {
                    sid += ".";
                } else {
                    sid += matrix[i][j];
                }
            }
        }
        System.out.println("ID: " + new BigDecimal(sid).doubleValue());
        return new BigDecimal(sid).doubleValue();
    }

I checked and the generated IDs are uniques.

Based on this, I tried to implement HashCode() as follows:

@Override
public int hashCode() {
    long bits = doubleToLongBits(id);
    int hash = (int) (bits ^ (bits >>> 32));
    System.out.println("hash: " + hash);
    return hash;
}

But my HashSet continues with duplicate values :( Does anyone have a suggestion about how to do this?

~~>EDIT

Sate class:

   public class State {

    public double id;
    public String[][] matrix;

    public State() {

    }

    public State(String[][] matrix) {
        this.matrix = createMatrix(matrix);//is created from a existing matrix
        this.id = generateId();
    }

    @Override
    public boolean equals(Object other) {
        if ((other == null) || !(other instanceof State)) {
            return false;
        }
        return ((State) other).getId().equals(this.getId()) && ((State) other).getId() == this.getId();
    }

    @Override
    public int hashCode() {
        long bits = doubleToLongBits(id);
        int hash = (int) (bits ^ (bits >>> 32));
        System.out.println("hash: " + hash);
        return hash;
    }

    public String toString() {
        return "Hashcode: " + this.hashCode();
    }

    public Double getId() {
        return id;
    }

    public void setId(Double id) {
        this.id = id;
    }

    public String[][] getMatrix() {
        return matrix;
    }

    public void setMatrix(String[][] matrix) {
        this.matrix = matrix;
    }

    public double generateId() {
        String sid = "";
        for (int i = 0; i < this.matrix[0].length; i++) {
            for (int j = 0; j < matrix[1].length; j++) {
                if (matrix[i][j].equals("0")) {
                    sid += ".";
                } else {
                    sid += matrix[i][j];
                }
            }
        }
        System.out.println("ID: " + new BigDecimal(sid).doubleValue());
        return new BigDecimal(sid).doubleValue();

    }

    private String[][] createMatrix(String[][] matriz) {
        String[][] copia = new String[matriz[0].length][matriz[1].length];
        for (int i = 0; i < copia[0].length; i++) {
            for (int j = 0; j < copia[1].length; j++) {
                copia[i][j] = matriz[i][j];
            }
        }
        return copia;
    }

Upvotes: 1

Views: 219

Answers (2)

RaffleBuffle
RaffleBuffle

Reputation: 5455

I would propose using the built-in methods of the Arrays class to generate a hashCode and test for equality:

@Override
public int hashCode() {
  return Arrays.deepHashCode(matrix);
}

@Override
public boolean equals(Object other) {
    if ((other == null) || !(other instanceof State)) {
        return false;
    }
   State s = (State)other;
   return Arrays.deepEquals(matrix, s.matrix);
}

Upvotes: -1

Axel M
Axel M

Reputation: 371

your problem is in the equals method,

you have to remove the last part:

 && ((State) other).getId() == this.getId();

you are checking if the Boolean has the same reference, but they don't need the reference to be equal, it's enough that there value is equal

Upvotes: 2

Related Questions