Jump to content
  • Advertisement
Sign in to follow this  
RLS0812

Testing If 2 Objects Are The Same

This topic is 845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there an easy way to compare 2 objects (of the same type) to see if all of the variables are the same?

 I'm having no luck with "==" or "Object.equals()" ...

 

Let's pretend this class has a lot of variables ...

public class Test {
	
	String a;
	int b;
	
	public Test(String a, int b){
		this.a = a;
		this.b = b;
		}
}

.

.

Here is how I test the 2 objects

public class Main {
	
	public static void print(String x){System.out.println(x);}
	
	public static void tryme(Test one,Test two){
		
		if ( one.equals(two) ){ print("True"); } 
		
		else{ print("False"); }
	}

	public static void main (String[] args) {
		
		Test test_a = new Test("Hello",1); 
		Test test_b = new Test("Hello",1);
		tryme(test_a,test_b);
		
		Test test_c = new Test("Hallo",2); 
		Test test_d = new Test("Hullo",3);
		tryme(test_c,test_d);
		}
}

Share this post


Link to post
Share on other sites
Advertisement
  @Override public boolean equals(Object o) {
    if (this == o) {return true;}
    if (o == null || !(o instanceof Test)) {return false;}
    Test tmp = (Test) o;
    return (this.b == tmp.b && Objects.equals(this.a, tmp.a) ); 

So the answer to my question is "no" ?

Share this post


Link to post
Share on other sites

If "easy" excludes writing an Test.equals method, the standard Java hook for comparing value equality, then indeed, there is no easy way.

 

It's a price you pay for extra flexibility. Testing all variables is just one notion of equality, you can have all kinds of equality notions (eg "have an equal number of nodes", "be in the same quadrant" or "on the same line", or whatever notion of equality you want).

Dropping down into Java code gives you that flexibility.

 

In general, you also write a hash function that matches with your notion of equality, and your objects then behave with your equality notion in eg sets or maps.

Edited by Alberth

Share this post


Link to post
Share on other sites

I find member-wise equality rarely necessary in the first place. A few notable exceptions are types that should semantically behave as regular value types, e.g. complex numbers, vectors etc. For such rare cases overriding equals() and getHashCode() to do member-wise comparison makes sense. In many other cases the problem can be simply avoided if you reconsider your approach. For instance, when you need to avoid duplicating a texture in memory, you can simply test the file name, rather than overriding equals() for the texture itself. Can you elaborate in what scenarios you need member-wise equality?

Edited by vanka78bg

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!