Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


how to compare two user defined objects in java?

Posted on 2011-03-08
Medium Priority
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 20

Accepted Solution

a_b earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

572 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