Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Set of user defined class

  1. #1
    TelliameD is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Question Set of user defined class

    I want to define a set of user defined class. e.g. let's say I've class defined as

    Java Code:
    public class point {
    int x,y;
    }
    
    Set<point> Points;
    
    Points = new HashSet<point>();
    Now I want to add arbitrary points to this set. So, I do

    Java Code:
    P = new point(x1,y1);
    Points.add(P);
    I expect duplicate points to be present in my data (i.e. I might have multiple objects which have the same x,y co-ordinates). If I try to add duplicate point objects to Points, I expect the addition to fail in the code above. But, that is not happening and duplicate objects are being added. What am I doing wrong? How do I fix it? What is the correct way to define a set over user defined class. Does my user defined class need to implement any interface/method, which can be used by the Set collection framework to identify duplicates? I've tried implementing the equals method as well as the Comparable<point> interface.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Set of user defined class

    To make it work correctly you have to implement both equals() and hashCode() methods in your Point class. This will make sure that your Set store Points with unique x and y value.
    Last edited by wsaryada; 07-05-2012 at 07:38 AM.
    TelliameD likes this.

  3. #3
    TelliameD is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Set of user defined class

    Quote Originally Posted by wsaryada View Post
    To make it work correctly you have to implement both equals() and hasCode() methods in your Point class. This will make sure that your Set store Points with unique x and y value.
    Thanks very much. this worked for me.

    But, why is the hashCode() method required. Why is implementing equals() not good enough?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Set of user defined class

    When we are working with the collection classes in Java we must provide a correct and consistent hash value for each equals object. If we don't provide a the hash it will be generated by the collection framework which result in a different hash code for an object that we considered to be equals. So to make it work as expected we must provide the hash code that is generated from the same properties that we used in the equals method.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Set of user defined class

    Not any collection.
    Just the ones that use Hashes (ie HashSet, HashMap).
    Similarly with equals and Sets.
    Trees require either implementing Comparable or a Comparator.
    It's all documented in the various APIs.

    Can I just suggest to the OP that they use naming standards?
    The class name and object names in the above code are back to front.
    Should be 'Point' for the class and 'points' for the variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem with user defined class
    By moosethmucha in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 05:58 PM
  2. how to write user defined class as Immutable?
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 07-04-2011, 11:50 PM
  3. Problem with user defined class in ArrayList
    By anders73 in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 03:59 PM
  4. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 01:02 AM
  5. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •