Solved

how to compare two user defined objects in java?

Posted on 2011-03-08
6
375 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
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.

 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

18 Experts available now in Live!

Get 1:1 Help Now