Solved

how to compare two user defined objects in java?

Posted on 2011-03-08
6
392 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 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