what is the purpose of hash code and equals method  in hashset

Posted on 2012-09-11
Last Modified: 2012-09-12
what is the purpose of hash code and equals method  in hashset
Question by:stmani2005
    LVL 37

    Expert Comment

    LVL 12

    Expert Comment

    by:Sharon Seth
    To that matter , hashCode() and equals() method in any Object , is a way of  specifying what it means for two objects to be equal .
    Say you have two integers 2 and 3 . You simply say they are not equal . But when it comes to Objects , say an Employee object , you have to very clearly put down when you can say two employee objects are equal (or not equal) . hashCode() and equals() are the methods that put down this logic
    LVL 16

    Accepted Solution

    hashCode() is a (hopefully) non-colliding 32 bit integer affording the object it refers to a unique handle. equals() is the way that handle gets compared to another object's hashCode, to determine if it is the same object or not. The same for HashSet, where it's used to determine if the object is itself a set - of the same size and with the same membership.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    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 …

    779 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