0

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!

Liumx31
  • 1,190
  • 1
  • 16
  • 33
  • 4
    IDEs can generate equals from fields for you. – akarnokd Jun 16 '22 at 20:50
  • If you use Intelij, you can go to code tab and click generate. There you have some options, one of them is equals method. – Cristian Cutitei Jun 16 '22 at 20:54
  • All major IDEs have an option for making `equals`, along with other useful methods like `hashCode` and `toString`. I've found that when you get an IDE that you've never used before, it's worth spending a little bit of time familiarising yourself with its main features, so that you don't end up reinventing wheels later. – Dawood ibn Kareem Jun 16 '22 at 21:04
  • *I'd rather not set the private fields to accessible at run time.* Are you suggesting they would be exposed to other callers that shouldn't have access, or am I reading too much into your question? – shmosel Jun 16 '22 at 21:05
  • 1
    so "*Efficient way to compare ...*" is meant to be "Efficient way to **write code** to compare..." ? (first one, as in title, would be something like better running time, or memory usage; second one, as in text, would be *easier* to type, or similar) – user16320675 Jun 16 '22 at 22:39
  • 1
    A `record` provides you with `hashCode` and `equals` implementations. But why does the amount of fields become a concern now, when you already wrote the field declarations, as well as getters? Not to speak of the hypothetical code actually using all of those values? The code of the `hashCode` and `equals` methods should be negligible compared to that. Unless, of course, this class is wrongly designed to begin with and a) an array or b) a map or c) not having so many properties would actually better suit your use case. – Holger Jun 17 '22 at 08:07

2 Answers2

0

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.

Cristian Cutitei
  • 120
  • 3
  • 12
0
  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;
}
Kaan
  • 5,434
  • 3
  • 19
  • 41