Hello, I'd like to create a function that accepts two objects and compares properties between them. Example

public boolean (obj1, obj2){
           if obj1.property1 = obj2.property2
                    return true
           else
                    return false
}

How can I do this?

Dani AI

Generated

wanted a method that takes objects (like readThis(String)) and compares their properties. was headed the right way showing typed parameters; correctly flagged shallow vs deep comparison and suggested equals(); and pointed to further reading on equality. Below are practical options and a small example you can apply right away.

If the two objects are instances of the same domain class, the cleanest approach is to implement equals() (and hashCode()) on that class so callers can simply do a.equals(b). A compact, null-safe pattern is shown here:

public class Table {
    private int id;
    private String name;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Table)) return false;
        Table other = (Table) o;
        return id == other.id && java.util.Objects.equals(name, other.name);
    }

    @Override
    public int hashCode() {
        return java.util.Objects.hash(id, name);
    }
}

If you only want to compare a subset of fields (schema vs data, say), write a dedicated comparator or helper method, or implement Comparator<Table> and use it where needed. For quick, ad-hoc comparisons across many classes use reflection-based helpers like Apache Commons EqualsBuilder.reflectionEquals, but be aware of performance and the possibility of comparing fields you didn’t intend.

Quick tips:

  • Use Objects.equals(a, b) for null-safe reference checks.
  • Don’t use == for object equality (it checks identity).
  • Always override hashCode() whenever you override equals().
  • For arrays and nested collections, use Arrays.deepEquals or explicit element-wise checks.

Reference: Java’s Object.equals contract and Objects utilities are useful background reading: Object.equals javadoc and java.util.Objects. For reflective comparisons see EqualsBuilder.reflectionEquals.

Recommended Answers

All 5 Replies

Is this just a syntax question? Because your psuedocode is (nearly) correct.

// MyObject is the class of the object you want to compare
public boolean(MyObject obj1, MyObject obj2){
    if (obj1.getSomeProperty() == obj2.getSomeProperty())
        return true;
    else
        return false
}

Or, more simply:

public boolean(MyObject obj1, MyObject obj2){
    return obj1.getSomeProperty() == obj2.getSomeProperty();
}

> Hello, I'd like to create a function
_______________________^^^^^

Method would be the correct terminology here.

> that accepts two objects and compares properties between them.

That largely depends on the type of those properties. If they are primitives, the comparison operator will do the job. If they are reference types, comparing their references doesn't serve much of a purpose. Then again it depends on whether you are in need of a deep or a shallow comparison.

If you are in need to comparing two objects of the same run-time type, consider overriding the equals() method of the Object class for your custom class. If you are in need of selective comparison of properties, look into the Comparator interface.

Also, explaining the real scenario rather than the end objective would fetch more helpful answers.

I just did that quickly, without paying too much attention to the syntax. My question isn't related on syntax rather on how to use objects as 'data types" sorta to use them as arguments. Let me give you another example

public void readThis(String read){
          System.out.println("The string to read is: " + read);
}

What I want is, instead of the argument being a string data type, I'd like the argument to be an object. The whole purpose of this is that I want to compare the properties of 2 objects. Say objects are tables, I want to input 2 tables and use the method I'm creating to determine if both objects are the same (have the same property values)

I hope that clear things up a little

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.