how to compare two user defined objects in java?

Posted on 2011-03-08
Last Modified: 2012-05-11

Please explain, how to compare two user defined objects in java, Kindly explain this with an example, because i have reffered so many articles, but still confusing.

Please do need full.

Question by:haneef_nb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

ID: 35069735
You use either a Comparator or make the object implement Comparable

Author Comment

ID: 35069739
suppose we have Employee object, if the empId is same, then we should not allow that object to add in collection....
LVL 86

Expert Comment

ID: 35069754
Yes. All you'd need for that is to use a Set. Return the empId as the value of hashCode and implements Employee.equals based on empId. Simple
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

LVL 20

Accepted Solution

a_b earned 500 total points
ID: 35069758
You will have to override the equals() method.

class Student
    pvt string firstName;
    pvt string lastName;
    pvt string addressName;

   public boolean equals(Object o)
        if(o instance of Student)
                Student s = (Student) o;
                if(firstName.equals(s.firstName) && lastName.equals(s.lastName) && addressName.equals(s.addressName))
                        return true;
return false;



I've missed out the getters and setters
LVL 92

Expert Comment

ID: 35074856
> if the empId is same, then we should not allow that object to add in collection....

you need to first check if the object is in the collection.
The actual comparison is done with the equals() method which you need to overrride as a_b has shown above. In that method you define when two objects are considered to be equal

Author Closing Comment

ID: 35206972

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question