获取非重写等于使用hashCode的方法

时间:2016-11-25 11:19:15

标签: java equals hashcode

我正在研究一个立方体求解器,它使用广度优先搜索来找到2x2x2 rubiks立方体求解器的最短解。问题是,在搜索中有重复的位置被击中,我的目标是知道我有多少,并在以后“修剪”它们。(我知道如何避免重复,但这与这篇文章无关) 。这是它应该是什么样子 Old version working

这是来自与hashCode和equals一起使用的代码的旧版本,但我试图让新版本与它一起使用。我认为原因是因为在旧版本中我重写了equals和hashCode,但在新版本中我似乎无法做到这一点,因为我不再比较对象,而是数组(猜测)。由于这个原因,当前版本没有重复。它说没有重复,但这是不正确的。

current non working version.

这是检测重复的旧版本的hashCode和equals是什么样的。

private Cube() {
    cube = new int[][] {
        { 0, 0, 0, 0 },
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 },
        { 5, 5, 5, 5 }
    };

    cube = scanCube(cube);
    cube = print_cube(cube);
}

private Cube(Cube other) {
    cube = new int[other.cube.length][];
    for (int i = 0; i < other.cube.length; i++) {
        cube[i] = Arrays.copyOf(other.cube[i], other.cube[i].length);
    }
}

public boolean isSolved() {
    for (int i = 0; i < cube.length; i++) {
        for (int k = 1; k < cube[i].length; k++) {
            if (cube[i][0] != cube[i][k]) {
                return false;
            }
        }
    }
    return true;
}

@Override
public boolean equals(Object other) {
    return other instanceof Cube && Arrays.deepEquals(((Cube) other).cube, cube);
}


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

这是当前版本。

public static void main(String[] args) {

    int[][] cube = new int[][] {
        { 0, 0, 0, 0 },
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 },
        { 5, 5, 5, 5 }
    };
    cube = scanCube(cube);
    cube = print_cube(cube);
    solve(cube);
}     
private static boolean isSolved(int [][] cube) {
    for (int i = 0; i < cube.length; i++) {
        for (int k = 1; k < cube[i].length; k++) {
            if (cube[i][0] != cube[i][k]) {
                return false;
            }
        }
    }
    return true;
}

public static int[][] copyCube(int [][] cube){
   int [][] copy = new int [6][4];

   for(int i = 0; i < 6; i++ ){
       copy[i] = cube[i].clone();
   }
   return copy;
}

public static boolean equals(int[][] other, int[][] cube) {
    return Arrays.deepEquals(other, cube);
}

public int hashCode(int [][] cube) {
    return Arrays.deepHashCode(cube);
}

在搜索方法中,确定重复项。这是旧代码。

static public void solve(Cube c) {
    Set<Cube> cubesFound = new HashSet<Cube>();
    cubesFound.add(c);

    Stack<Cube> s = new Stack<Cube>();
    s.push(c);

    Set<Stack<Cube>> initialPaths = new HashSet<Stack<Cube>>();
    initialPaths.add(s);

    solve(initialPaths, cubesFound);
}

static public void solve(Set<Stack<Cube>> livePaths, Set<Cube> cubesFoundSoFar) {
    System.out.println("livePaths size:" + livePaths.size());
    int numDupes = 0;

    Set<Stack<Cube>> newLivePaths = new HashSet<Stack<Cube>>();

    for (Stack<Cube> currentPath : livePaths) {

        Set<Cube> nextStates = currentPath.peek().getNextStates();

        for (Cube next : nextStates) {
            if (currentPath.size() > 1 && next.isSolved()) {
                currentPath.push(next);
                System.out.println("Path length:" + currentPath.size());
                System.out.println("Path:" + currentPath);
                System.exit(0);

            } else if (!cubesFoundSoFar.contains(next)) {
                Stack<Cube> newCurrentPath = new Stack<Cube>();
                newCurrentPath.addAll(currentPath);
                newCurrentPath.push(next);
                newLivePaths.add(newCurrentPath);
                cubesFoundSoFar.add(next);
            } else {
                numDupes += 1;
            }
        }
    }

    System.out.println("Duplicates found " + numDupes + ".");
    solve(newLivePaths, cubesFoundSoFar);
}

新的。

static private void solve(int[][] cube) {

    int[][][] s = new int[12][6][4];
    s[0] = cube;

    Set<int[][][]> initialPaths = new HashSet<int[][][]>();
    initialPaths.add(s);
    Set<int[][]> cubesFound = new HashSet<int[][]>();
    cubesFound.add(cube);

    solve(initialPaths, cubesFound, 1);
}

static private void solve(Set<int[][][]> livePaths,Set<int[][]> cubesFoundSoFar, int iterationCount) {
    System.out.println("livePaths size:" + livePaths.size());

    Set<int[][][]> newLivePaths = new HashSet<int[][][]>();
    int counter = 0;
    int recordDepth = 0;
    int duplicates = 0;

    for(int[][][] currentPath : livePaths) {

        Set<int [][]> nextStates = getNextStates(currentPath[iterationCount-1]);

        for (int[][] next : nextStates) {
            if (isSolved(next)) {
                currentPath[iterationCount] = next;
                int maxSteps = -1;
                System.out.println("Path:" );
                for(int i = 0; i < currentPath.length; i++) {
                    if(currentPath[i] != null) {
                        maxSteps = i;
                        System.out.println(toString(currentPath[i]));
                    }else {
                        break;
                    }
                }
                System.out.println("Path length:" + maxSteps);
                System.exit(0);

            } else if(!cubesFoundSoFar.contains(next)){
                int[][][] newCurrentPath = new int[12][6][4];
                newCurrentPath = currentPath.clone();
                newCurrentPath[iterationCount] = next;
                newLivePaths.add(newCurrentPath);
                counter ++;
                cubesFoundSoFar.add(next);
            }  else {
                duplicates += 1;
            }
        }
    }
    //System.out.println(" Set.size(): "+newLivePaths.size());

    String storeStates = "positions.txt";
    try {
        PrintWriter outputStream = new PrintWriter(storeStates);
        outputStream.println(storeStates);
        for(int[][][] s:newLivePaths) {
            outputStream.println(toString(s[iterationCount]));
        }
        outputStream.close();

    } catch (FileNotFoundException e) {
        System.err.println("Fatal: could not open cache file for cube positions. exiting.");
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println("Duplicates found "+ duplicates + ".");
    solve(newLivePaths, cubesFoundSoFar, iterationCount+1);
}

1 个答案:

答案 0 :(得分:1)

您没有在第二个代码中覆盖equals(Object)方法,但是 Set.contains(Object)使用equals来比较元素。由于Cube中没有,因此使用Object之一。这不会比较内容,只是测试对象是否是同一个实例(相同的内存位置)。

这里是contains的相关部分:

  

...更正式地说,当且仅当此集合包含元素e时才返回true(o == null?e == null: o.equals(e))。 ...

您可以添加类似于第二个代码的内容:

@Override
public boolean equals(Object other) {
    if (other instanceof Cube)
        return equals(cube, ((Cube) other).cube);
    else
        return false;
}


@Override
public int hashCode() {
    return hashCode(cube);
}