Solved

how to compare two user defined objects in java?

Posted on 2011-03-08
6
371 Views
Last Modified: 2012-05-11
Hi,

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.

Thanks....
0
Comment
Question by:haneef_nb
6 Comments
 
LVL 86

Expert Comment

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

http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html
0
 

Author Comment

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

Expert Comment

by:CEHJ
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Accepted Solution

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

Eg.
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
0
 
LVL 92

Expert Comment

by:objects
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
0
 

Author Closing Comment

by:haneef_nb
ID: 35206972
Thanks..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now