关于Java集合,如何根据元素的属性删除元素?

关于Java集合,如何根据元素的属性删除元素?,java,set,Java,Set,我的代码中有一个HashSet,它使用一个自定义类型Line,其中Line有四个整数字段(x1、y1、x2、y2;都表示行的起点和终点的坐标)。我用大量这样的行填充HashSet。然后我想稍后删除特定的行。我尝试调用HashSet.remove(新行(正确的属性)),但失败了。有没有关于我该怎么做的想法 随附的代码仅供参考。该类试图实现一个Aldous Broder迷宫生成器,以便在传递给绘图机制之前,首先将所有墙填充到集合中,然后移除墙(当该方法雕刻迷宫路径时) package tests;

我的代码中有一个HashSet,它使用一个自定义类型Line,其中Line有四个整数字段(x1、y1、x2、y2;都表示行的起点和终点的坐标)。我用大量这样的行填充HashSet。然后我想稍后删除特定的行。我尝试调用HashSet.remove(新行(正确的属性)),但失败了。有没有关于我该怎么做的想法

随附的代码仅供参考。该类试图实现一个Aldous Broder迷宫生成器,以便在传递给绘图机制之前,首先将所有墙填充到集合中,然后移除墙(当该方法雕刻迷宫路径时)

package tests;

import java.util.HashSet;
import java.util.Random;

public class AldousBroderTest {

static HashSet<Line> walls = new HashSet<Line>();
static Random rn = new Random();

public static void generateWalls(int x1, int y1, int x2, int y2){
    for (int i = x1; i < x2; i += 10){
        for (int j = y1; j < y2; j += 10){
            walls.add(new Line(i, j, i + 10, j));
            walls.add(new Line(i,j,i,j+10));
        }
    }
    walls.add(new Line(x1, y1, x1, y2));
    walls.add(new Line(x1, y1, x2, y1));
    walls.add(new Line(x2, y1, x2, y2));
    walls.add(new Line(x1, y2, x2, y2));
}

public static void generateMaze(int x1, int y1, int x2, int y2){
    boolean[][] visited = new boolean[x2-x1][y2-y1];
    int counter = 1;
    int currentx = rn.nextInt((x2-x1)/10)*10;
    int currenty = rn.nextInt((y2-y1)/10)*10;
    visited[currentx][currenty] = true;
    int cellcount = (x2-x1)/10 * (y2-y1)/10;
    System.out.println(cellcount);
    while (counter < cellcount){
        int direction = rn.nextInt(4); 
        switch (direction){
        case 0: 
            if(currenty == y1){break;}
            currenty -= 10;
            if(visited[currentx][currenty] == false){
                visited[currentx][currenty] = true;
                counter++;
                walls.remove(new Line(currentx, currenty+10, currentx+10, currenty+10));
            }
            break;
        case 1:
            if(currentx+10 == x2){break;}
            currentx += 10;
            if(visited[currentx][currenty] == false){
                visited[currentx][currenty] = true;
                counter++;
                walls.remove(new Line(currentx, currenty, currentx, currenty+10));
            }
            break;
        case 2:
            if(currenty+10 == y2){break;}
            currenty += 10;
            if(visited[currentx][currenty] == false){
                visited[currentx][currenty] = true;
                counter++;
                walls.remove(new Line(currentx, currenty, currentx+10, currenty));
            }
            break;
        case 3:
            if(currentx == x1){break;}
            currentx -= 10;
            if(visited[currentx][currenty] == false){
                visited[currentx][currenty] = true;
                counter++;
                walls.remove(new Line(currentx+10, currenty, currentx+10, currenty+10));
            }
            break;
        }
    }
}

public static void main(String[] args){
    generateWalls(0,0,50,50);
    generateMaze(0,0,50,50);
    Frame frame = new Frame(walls);
  }

}
包测试;
导入java.util.HashSet;
导入java.util.Random;
公共类AldousBroderTest{
静态HashSet walls=新HashSet();
静态随机rn=新随机();
公共静态无效生成墙(int x1、int y1、int x2、int y2){
对于(int i=x1;i
在类行中,重写equals和hascode方法

@Override
public boolean equals(Object obj) {
    // equals code
}

@Override
public int hashCode() {
    // hascode method
}

在这里,您可以在class Line、override equals和hascode方法中找到“为什么要实现这两个方法”的解释

@Override
public boolean equals(Object obj) {
    // equals code
}

@Override
public int hashCode() {
    // hascode method
}

在这里,您可以找到“为什么要实现这两个方法”的解释。

您是
HashSet
在添加对象时对其进行散列,一个
新行(…)
调用将生成一个新对象,从而生成一个不同的散列代码;因此它不起作用。要删除这些对象,您必须保留原始对象并使用它们进行删除,或者像其他人所说的那样,在
类中重写
equals
hashCode
方法,以便它们基于对象的属性。

您是
HashSet
在添加对象时对其进行哈希运算,一个
新行(…)
调用将产生一个新对象,从而产生一个不同的hashcode;因此它不起作用。要删除这些对象,您必须保留原始对象并使用这些对象进行删除,或者像其他人所说的那样,在
类中重写
equals
hashCode
方法,以便它们基于对象的属性。

您正在尝试删除新对象吗<代码>墙。移除(新行(currentx,currenty+10,currentx+10,currenty+10))
Line
是否实现hashcode和equals?为了解释,是的,我正计划尝试创建一个具有相同属性的新行,看看这是否可行。我还没有意识到覆盖equals方法,这很有意义。您正在尝试删除一个新对象吗<代码>墙。移除(新行(currentx,currenty+10,currentx+10,currenty+10))Line是否实现hashcode和equals?为了解释,是的,我正计划尝试创建一个具有相同属性的新行,看看这是否可行。我还没有意识到要重写equals方法,这很有意义。啊,好的,这很有意义,谢谢。我是否也需要重写hashCode,或者重写equals就足够了?(若要检查-equals,则取另一个Line实例,如果它等于当前实例,则返回true?)您必须覆盖这两个实例,解释存在链接问题,如果equals方法中指定的条件匹配或不匹配,则yes equals取另一个Line实例,并返回true或false,阅读更多关于如何编写equals方法编辑的链接(因为某些原因,所以不允许我编辑,我是新来的,抱歉):刚刚看到我错过的一行,以及需要两者的解释,现在明白了。谢谢啊,好的,这很有道理,谢谢你。我是否也需要重写hashCode,或者重写equals就足够了?(及