Java HashMap/Hashtable 不检索自己的密钥

Java HashMap/Hashtable does not retrieve its own keys

对于一个学校项目,我目前正在编写一个程序,该程序基于以下来源编写了一个学习如何玩 TicTacToe 的程序,该程序基于以下来源:https://github.com/aimacode/aima-java/blob/AIMA3e/aima-core/src/main/java/aima/core/learning/reinforcement/agent/QLearningAgent.java

但我发现了一个更具体的问题,尽管我进行了所有研究和调试,但我无法解决它。

算法使用HashMap来存储Q-Values,这里是它的定义:

Map<Pair<S, A>, Double> Q = new Hashtable<Pair<S, A>, Double>();

然后,我实现了 classes,其中 S 是一个 Grid,A 是一个 DynamicAction,我具体使用了 TicTacToeAction 和 NoOpAction 的实例,它们都扩展了 DynamicAction。

这是网格的 hashCode 和 equals :

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((matrix == null) ? 0 : matrix.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Grid other = (Grid) obj;
    if (matrix == null) {
        if (other.matrix != null)
            return false;
    } else {
        System.out.println("this : " + this);
        System.out.println("other : " + other);
        if (!matrix.equals(other.matrix)) {
            return false;
        }
    }
    System.out.println("this and other are equals");
    return true;
}

矩阵定义如下:

private ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();

对于 TicTacToeAction 它有点复杂,因为它扩展了 DynamicAction,它本身扩展了一个 class ObjectWithDynamicAttributes,它实现了 hashCode 和 equals,但是那部分代码来自 GitHub在上一个 link.

对于 Pair,hashCode 和 equals 的简单定义如下:

@Override
public boolean equals(Object o) {
    if (o instanceof Pair<?, ?>) {
        Pair<?, ?> p = (Pair<?, ?>) o;
        return a.equals(p.a) && b.equals(p.b);
    }
    return false;
}

@Override
public int hashCode() {
    return a.hashCode() + 31 * b.hashCode();
}

但是当程序尝试使用 QLearningAgent class 中的 putget 方法时,我遇到了一些大问题。它补充说它很少检索以前输入的值,更糟糕的是,keySet() 似乎包含重复的元素!此外,由于 equals 方法中的打印,我看到如果 putget 方法没有检查整个 KeySet 就有点像查看该条目是否存在,所以相同的条目被一次又一次地放入...但是,当我尝试将值手动放入一个类似的 HashMap 中时,用相同的 classes 定义,这样的问题似乎没有发生了。

也许最难以理解的例子是这个:

这部分代码:

    for(Pair<S, A> pair : Q.keySet()) {
        System.out.println(pair);
        System.out.println("Contains? " + Q.keySet().contains(pair));
        System.out.println("value : " + Q.get(pair));
    }

Returns 这个(除其他外...):

< -------------
| 1 | O | X |
-------------
| 4 | X | O |
-------------
| X | O | X |
-------------
 , Action[name=Play Cell 6] > 
Contains? false
value : null
< -------------
| 1 | O | X |
-------------
| 4 | X | O |
-------------
| X | O | X |
-------------
 , Action[name=Play Cell 6] > 
Contains? false
value : null

所以,首先,这两对看起来相等,但不能根据集合的定义,但是 HashMap 的 KeySet 怎么可能(用 HashTable 替换它的结果相同,不能承认 null值所以 "value : null" 意味着地图中不存在这样的条目)不检索直接来自 Q.keySet() 的对?因为即使在 equals 方法中存在我没有看到的问题,在这种情况下使用 contains 就像在某个时候使用 equals 将实例与自身进行比较,不是吗?所以我想第一个 "this == obj" 条件会被检查。

我一定是遗漏了什么,但是我尝试了很多东西,我没有更多的想法,所以我希望有人能够帮助我...

感谢您的宝贵时间,

保罗

编辑 1:按照@tevemadar 的建议,我添加打印以显示哈希码。

    for(Pair<S, A> pair : Q.keySet()) {
        System.out.println(pair);
        System.out.println("Contain? " + Q.keySet().contains(pair));
        System.out.println("value : " + Q.get(pair));
        System.out.println("Pair hashCode : " + pair.hashCode());
        System.out.println("Grid hashCode : " + pair.getFirst().hashCode());
        System.out.println("Action hashCode : " + pair.getSecond().hashCode());
    } 

这是一个返回的例子:

< -------------
| 1 | X | O |
-------------
| 4 | O | X |
-------------
| O | X | O |
-------------
 , Action[name=Play Cell 3] > 
Contain? false
value : null
Pair hashCode : 1710044996
Grid hashCode : 79268846
Action hashCode : -224488982
< -------------
| 1 | X | O |
-------------
| 4 | O | X |
-------------
| O | X | O |
-------------
 , Action[name=Play Cell 3] > 
Contain? false
value : null
Pair hashCode : 1710044996
Grid hashCode : 79268846
Action hashCode : -224488982

因此,属于 keySet() 的两个元素似乎也具有相同的哈希码。

实际上,这是因为本例中的键 Pair 是可变的,您可以更改它的内部属性。

考虑下面的示例,我们有两个键,具有不同的 'names',然后 .put 将不会看到它们相同,因为它们具有不同的 hashCode,但是,类似于在您的情况下,您似乎更改了该对内的属性,在这种情况下,地图不会删除重复项,因为它仅在 put.

上执行此操作
class MyAction extends DynamicAction {
    public MyAction(String name) {
        super(name);
    }
}

@Test
public void test() {
    Map<Pair<String, MyAction>, Double> Q = new Hashtable<Pair<String, MyAction>, Double>();
    Pair<String, MyAction> pair1 = new Pair<String, MyAction>(
            "Play Cell", new MyAction("something1"));
    Pair<String, MyAction> pair2 = new Pair<String, MyAction>(
            "Play Cell", new MyAction("something2"));

    // different hashcodes
    System.out.println(pair1.hashCode());
    System.out.println(pair2.hashCode());
    Q.put(pair1, 1d);
    Q.put(pair2, 1d);
    // so the size is 2
    System.out.println(Q.size());

    // however, we can change the hashcode of the key afterwards
    System.out.println("Setting attributes");
    pair2.getSecond().setAttribute(DynamicAction.ATTRIBUTE_NAME, "something1");
    for (Object o : Q.keySet()) {
        Pair p = (Pair) o;
        System.out.println(p.hashCode());
    }
    // and the size is still 2
    System.out.println(Q.size());
}