将对象与许多(私有)变量进行比较的有效方法

发布于 2025-02-07 22:18:56 字数 774 浏览 1 评论 0原文

我有一个类,其中有一系列类似的私人变量列表,

class ManyVariables {
    private int var1;
    private int var2;
    //....
    private int var49;
    
    public int getVar1(){};
    public int getVar2(){};
    //....
    public int getVar49(){};
}

我试图覆盖equals方法,以便它比较两个实例的所有变量,但是,由于变量列表很长将它们全部输入手动真的不是理想的。有没有办法将它们全部纳入stream对象并使用stream的管道进行比较?我知道我还可以使用 feflection>我宁愿不要将私有字段设置为在运行时可访问。谢谢!

I have a class that with a long list of private variables like this

class ManyVariables {
    private int var1;
    private int var2;
    //....
    private int var49;
    
    public int getVar1(){};
    public int getVar2(){};
    //....
    public int getVar49(){};
}

I am trying to override the equals method so that it compares all the variables of two instances, however, since the list of variables is so long typing them all out manually is really not ideal. Is there a way to get them all into a Stream object and use Stream's pipeline to do the comparison? I know I can also use reflection but I'd rather not set the private fields to accessible at run time. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋凉 2025-02-14 22:18:56

因此,您要么构建一种方法,如前所述,通过IDE生成它,要么将其放入JSON对象中并比较字符串。我看不到任何其他方式。

有一些库将您的对象转换为JSON或任何其他字符串格式。甚至ToString()方法也可能起作用。但是要小心,因为最后两个提到的解决方案仅比较简易值。

So you either build a method as you stated earlier, generate it through an IDE or put it inside a JSON object and compare the strings. I do not see any other way.

There are libraries that convert your object into JSON or any other string format. Even the toString() method might work. But be careful as these last 2 mentioned solutions only compare easy values.

苏璃陌 2025-02-14 22:18:56
  1. 使用Intellij或Eclipse之类的IDE。我将在此答案中使用Intellij。
  2. 打开.java 类mulanvariables
  3. 将光标放在类中的某个位置,
  4. 请转到菜单:“代码”:“代码”,然后
  5. 在弹出窗口中“生成”, 在第一个提示中选择“ equals()和hashcode()”
  6. 在第一个提示equals()中,验证所有字段已被选中(选中所有字段),然后在第二个提示中单击hashcode()的“ next”
  7. ,再次检查所有字段,然后“完成”

这是一个示例,从此类别开始:

class Example {
    private int a;
    private int b;
    private int c;
}

使用自动生成后,I有:

class Example {
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Example example = (Example) o;

        if (a != example.a) return false;
        if (b != example.b) return false;
        return c == example.c;
    }

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

    private int a;
    private int b;
    private int c;
}
  1. Use an IDE like IntelliJ or Eclipse. I'll use IntelliJ in this answer.
  2. Open the .java file for class ManyVariables
  3. Put the cursor somewhere inside the body of the class
  4. Go to menu: "Code", then "Generate"
  5. In the pop-up, choose "equals() and hashCode()"
  6. In the first prompt for equals(), verify all of the fields are checked (check the boxes if any are not already checked), then click "Next"
  7. In the second prompt for hashCode(), again verify all fields are checked, then "Finish"

Here's an example, starting with this class:

class Example {
    private int a;
    private int b;
    private int c;
}

After using the automatic generation, I have this:

class Example {
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Example example = (Example) o;

        if (a != example.a) return false;
        if (b != example.b) return false;
        return c == example.c;
    }

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

    private int a;
    private int b;
    private int c;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文