简体   繁体   中英

Using a customed class as keys to in HashMap but not able to search the keys

I created a HashMap that uses a customed class Location as keys. After inserting all the entries into the HashMap using put() , I am not able to search the keys.

I have tried to use get() or containsKey() to search, but neither give me positive results. However, I am sure that the keys do exists in the code because I have used HashMap iteration to print out the keys.

Below is the code:

public HashMap<Location, Integer>beenTo = new HashMap<>();
public int uniquePathsIII(int[][] grid) {
        for (int i=0; i<grid.length; i++){
             for (int j=0; j<grid[0].length; j++){
                 if (grid[i][j] == 0 || grid[i][j] == 2){
                     Location newSquare = new Location(i,j);
                     notBeen.put(newSquare, 1);                    
                 }           
             }
        }
        Location newSquare = new Location(0,1);
        if (notBeen.get(newSquare) != null){
             return 10;
         }
        if (notBeen.isEmpty()){
            return -1;
        }
}

Below is the class Location:

class Location{
        int i;  // row  
        int j;  // column
        public Location(int _i, int _j){
            i = _i;
            j = _j;
        }
        public int getI(){
            return i;
        }
        public int getJ(){
            return j;
        }
        public void setI(int _i){
            i = _i;
        }
        public void setJ(int _j){
            j = _j;
        }
}

In the above code, I wanted to search for the key Location(0,1) . I have made sure that the Hashmap notBeen is not empty, and tried that the key does exist. But I am never able to find it using containsKey() nor get() .

You need to implement/override hashCode and equals methods if you want a custom Object to work as a key in a HashMap in Java.

FYI: _variableName goes against Java naming conventions Oracle Java Naming Convention . It is also not necessary as you can get the same result using:

public Location(int i, int j){
  this.i = i;
  this.j = j;
}

In order to avoid memory leaks map's key must be immutable object,and improve search key speed,the key object has equals() and hashcode() methods is good practice(first use hashcode judge equals then use equals() method).In your code it is best to make location immutable(not support setter methods).

public class Location {
    private final int i;
    private final int j;

    public Location(int x, int y) {
        this.i = x;
        this.j = y;
    }

    public int getI() {
        return i;
    }

    public int getJ() {
        return j;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Location)) {
            return false;
        }
        Location point = (Location) o;
        return i == point.i &&
            j == point.j;
    }

    @Override
    public int hashCode() {
        return Objects.hash(i, j);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM