1 2 Previous Next 22 Replies Latest reply: Aug 6, 2011 4:04 PM by 796440 Go to original post RSS
      • 15. Re: hashcode method in Object class?
        796440
        JavaFunda wrote:
        Is there any disadvantage in using the default hashcode and equals method by object class?
        If you want to compare instances of your class for equality by the value of one or more fields, or use them in a hash-based data structure, then the disadvantage is that it won't work. If you're not going to do those things, then there's no need to override hashCode(). and equals().

        If other words: If you need other than the default behavior, you have to override. If you don't need it, you don't have to. Not sure why that's such a hard concept to grasp.
        I understand when we require some specific functionality like we want the last employee object in case first name is same. In that case we should override the hashcode and equals method based on last name.But otherwise we can use default hashcode and equals method provided by object class. Right?
        You're just repeating yourself here. I still don't understand what you're saying, ("we want the last employee object in case first name is same" ??? WHAT???) but everything I said previously still applies. I think you're making this way more complicated than it actually is.
        • 16. Re: hashcode method in Object class?
          866541
          If you want to compare instances of your class for equality by the value of one or more fields, or use them in a hash-based data structure, then the disadvantage is that it won't work. If you're not going to do those things, then there's no need to override hashCode(). and equals().
          Hi jverd, My intention is to use the class in a hash-based data structure. So what i am trying to say here is if i dont override the hashcode() and equals() method in my class default implementation of hashcode(i.e by converting the internal address of the object into an integer) and default implementation of equals method(i.e it will return true if x == y has the value true where x and y represent objects) will apply.Right?


          Though if i have some special requirement for hashcode and equals implementation in that case only i need overriding of these methods.
          • 17. Re: hashcode method in Object class?
            Kayaman
            JavaFunda wrote:
            If you want to compare instances of your class for equality by the value of one or more fields, or use them in a hash-based data structure, then the disadvantage is that it won't work. If you're not going to do those things, then there's no need to override hashCode(). and equals().
            Hi jverd, My intention is to use the class in a hash-based data structure. So what i am trying to say here is if i dont override the hashcode() and equals() method in my class default implementation of hashcode(i.e by converting the internal address of the object into an integer) and default implementation of equals method(i.e it will return true if x == y has the value true where x and y represent objects) will apply.Right?
            Yes. If you don't override equals() and hashCode(), the default implementations will be inherited from the superclass.
            And as jverd said, if you intend to do equality tests for those objects or put them into hash-based data structures, you'll notice that without overriding those methods your programs will not work properly.
            Though if i have some special requirement for hashcode and equals implementation in that case only i need overriding of these methods.
            No. You don't need to have special requirements for those methods. If you want to compare your objects for equality, you'll need to implement equals() and if you want to put your objects into for example HashSets or as keys in a HashMap, you'll need to override hashCode().

            You should be doing more reading of tutorials, javadocs and other lessons and less endless repeating of your questions on the forums.
            • 18. Re: hashcode method in Object class?
              YoungWinston
              JavaFunda wrote:
              Though if i have some special requirement for hashcode and equals implementation in that case only i need overriding of these methods.
              And I think you'll find that in many cases that's precisely what you want; especially for something like an Employee.

              If you don't provide some notion of 'logical equality' then you can add as many "identical" Employees to a Map or Set as you like, simply by creating them as different objects. The main difference with a hashed collection is that the value returned by <tt>hashCode()</tt> determines the distribution of objects, and that's why it's good to know some basic techniques for calculating hashcodes that provide (a) a good spread, and (b) distinct values for each "equal" object.

              Note that overriding equals() and hashCode() doesn't lose you the ability to store objects by their "native" hashcode if you want. Have a look at <tt>IdentityHashMap</tt>.

              Winston
              • 19. Re: hashcode method in Object class?
                796440
                JavaFunda wrote:
                If you want to compare instances of your class for equality by the value of one or more fields, or use them in a hash-based data structure, then the disadvantage is that it won't work. If you're not going to do those things, then there's no need to override hashCode(). and equals().
                Hi jverd, My intention is to use the class in a hash-based data structure.
                Then you must override hashCode() and equals() in a manner that is appropriate for your needs, and such that the two methods are consistent as described above and in the docs I suggested you read.
                So what i am trying to say here is if i dont override the hashcode() and equals() method in my class default implementation of hashcode(i.e by converting the internal address of the object into an integer) and default implementation of equals method(i.e it will return true if x == y has the value true where x and y represent objects) will apply.Right?
                Those impelmentations will be used. There's nothing magical or special about these methods. They're exactly like any other non-final public method. If you override them in a subclass, the subclass's method will beused, and if you don't override them, the parent's method will be used.
                Though if i have some special requirement for hashcode and equals implementation in that case only i need overriding of these methods.
                Exactly like any other method. If you need a different implementation, you override it, and if not, you don't.

                Again, although these methods are common and are called from within the core API, there is nothing special or magical about them.
                • 20. Re: hashcode method in Object class?
                  796440
                  In addition to my previous comments, here are a couple of corrections to what you said:
                  JavaFunda wrote:
                  if i dont override the hashcode() and equals() method in my class default implementation of hashcode(i.e by converting the internal address of the object into an integer)
                  Yes it will use the "default" (Object's) implementation of hashCode(), however, you can't assume the address will have anything to do with it. It may or may not, be we don't know and we don't care what the actual implementation does.
                  and default implementation of equals method(i.e it will return true if x == y has the value true where x and y represent objects) will apply.Right?
                  Yes, the non-overridden Object's equals() method will return true if and only if x == y, but x and y do not represent objects, because == cannot be applied to objects. x and y are variables that hold references, and x == y is true if the references have the same value. An implication of them having the same value is that they point to the same object, but the object does not come into play at all when doing x == y, just the references.
                  • 21. Re: hashcode method in Object class?
                    maheshguruswamy
                    JavaFunda wrote:
                    If you want to compare instances of your class for equality by the value of one or more fields, or use them in a hash-based data structure, then the disadvantage is that it won't work. If you're not going to do those things, then there's no need to override hashCode(). and equals().
                    Hi jverd, My intention is to use the class in a hash-based data structure. So what i am trying to say here is if i dont override the hashcode() and equals() method in my class default implementation of hashcode(i.e by converting the internal address of the object into an integer) and default implementation of equals method(i.e it will return true if x == y has the value true where x and y represent objects) will apply.Right?


                    Though if i have some special requirement for hashcode and equals implementation in that case only i need overriding of these methods.
                    Why don't you convert your requirements into code and write some junit unit tests around it to test equality. If any of your tests return unexpected values, come back here and post them. At this point, you should have enough information to start writing some code.
                    • 22. Re: hashcode method in Object class?
                      796440
                      maheshguruswamy wrote:
                      At this point, you should have enough information to start writing some code.
                      Plus. Plus.
                      1 2 Previous Next