Solved

how to compare two user defined objects in java?

Posted on 2011-03-08
6
384 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 63
javap bin 2 34
HashTable highest marks enumeration alternative 9 37
servlet filter example 37 61
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

770 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