相关疑难解决方法(0)

Java HashMap可以正常工作,但containsKey却没有

我试图在HashMap中找到一个键.我可以使用'get'打印所选键,但是当我在if语句中使用'containsKey'时,找不到它.

我知道密钥存在于Map中,但它一直返回false.人们有什么想法?

我的代码:

public static boolean checkLowerStructuralSupport(Location location) {

    boolean hasSupport = false;

    Location supportingLocation = new Location(location.getX(), location.getY(), location.getZ() - 1);

    System.out.println(_levels.get(supportingLocation.getZ()).getLevelSites2().get(supportingLocation)); //works

    if (_levels.get(supportingLocation.getZ()).getLevelSites2().containsKey(supportingLocation)) {
        hasSupport = true;
    } else {
        hasSupport = false;
    }

    return hasSupport;
}
Run Code Online (Sandbox Code Playgroud)

以下是Location类的代码:

public class Location {

    protected int _x;
    protected int _y;
    protected int _z;

    public Location(int xAxis, int yAxis, int zAxis) {
        this._x = xAxis;
        this._y = yAxis;
        this._z = zAxis;
    }

    public void equals() {
        //not implemented yet
    } …
Run Code Online (Sandbox Code Playgroud)

java hashmap

14
推荐指数
2
解决办法
4万
查看次数

标签 统计

hashmap ×1

java ×1