10 Replies Latest reply: Jun 20, 2011 3:33 PM by EJP RSS

    why there are equal and hashcode methods are there in collection ?

    838458
      hi,

      why there are equal and hashcode methods are there in collection ? Still they are present in the Object class.
        • 1. Re: why there are equal and hashcode methods are there in collection ?
          tschodt
          Do you understand what they are for? (in the context of a hashed collection)
          http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode%28%29
          • 2. Re: why there are equal and hashcode methods are there in collection ?
            838458
            Ya .. I know for what purpose they are used ... but my question is why they are in Collection Interface as well as in Object class. Object is the super class for all the classes right?Then i can override those ?

            but still they are in Collection why?
            • 3. Re: why there are equal and hashcode methods are there in collection ?
              EJP
              I suspect it is so they can provide the extra Javadoc that appears in Collection.
              • 4. Re: why there are equal and hashcode methods are there in collection ?
                836548
                I think it is because, the collections represent the group of objects. And to take the advantage of object method, the collection interface has these methods.
                • 5. Re: why there are equal and hashcode methods are there in collection ?
                  796440
                  833545 wrote:
                  I think it is because, the collections represent the group of objects. And to take the advantage of object method, the collection interface has these methods.
                  Nope. It's for the documentation, as previously stated. Every interface has those methods anyway, even if it doesn't declare them explicitly in its source code. Even if they weren't explicitly declared in Collection, those methods would be available though every Collection reference.

                  [url http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.4.4]JLS 6.4.4 The Members of an Interface Type says: "If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface."

                  Therefore, the following is legal. I can call Object's methods on an "I" reference even though interface "I" does not declare them.
                  public class Z {
                    public static void main(String[] args) {
                      I i = new I() {};
                      i.toString();
                      i.equals(null);
                      i.hashCode();
                  
                    } 
                  }
                  
                  interface I {
                  }
                  Edited by: jverd on Jun 18, 2011 1:30 PM
                  • 6. Re: why there are equal and hashcode methods are there in collection ?
                    EJP
                    I think it is because, the collections represent the group of objects. And to take the advantage of object method, the collection interface has these methods.
                    You haven't read the question properly. It wouldn't be for that reason because, as the OP said, all objects have those methods whether specified or not, so your explanation doesn't explain anything.
                    • 7. Re: why there are equal and hashcode methods are there in collection ?
                      796440
                      EJP wrote:
                      I think it is because, the collections represent the group of objects. And to take the advantage of object method, the collection interface has these methods.
                      You haven't read the question properly. It wouldn't be for that reason because, as the OP said, all objects have those methods whether specified or not, so your explanation doesn't explain anything.
                      His explanation would make sense only if the JLS snippet I quoted above were absent, such that we wouldn't be able to call those methods through an interface reference. At least, that's what I assume he was talking about. If that's not the bogus assumption under which he was operating, then I don't know what he means.
                      • 8. Re: why there are equal and hashcode methods are there in collection ?
                        836548
                        Jverd i agree with your explaination, but here i mean:

                        As we all know the collection interface is meant to deals with objects, and it has provided these methods to check whether the objects in two collection interfaces are same. And not for extra docs.

                        For example if you have two arraylists instances populated with objects, Then you could use equal method to check whether these two are same, instead of checking equality of each object in it.
                        • 9. Re: why there are equal and hashcode methods are there in collection ?
                          796440
                          833545 wrote:
                          Jverd i agree with your explaination, but here i mean:

                          As we all know the collection interface is meant to deals with objects, and it has provided these methods to check whether the objects in two collection interfaces are same. And not for extra docs.
                          Incorrect.

                          The methods already exist in Collection even if it doesn't explicitly declare them. The Collection interface doesn't provide them. Every bit of code in the Collections Framework, and every bit of code that uses the Collections Framework would behave identically if those methods were not there in the interface.

                          They are only there for documentation. They provide nothing functionally/behaviorally.
                          For example if you have two arraylists instances populated with objects, Then you could use equal method to check whether these two are same, instead of checking equality of each object in it.
                          Incorrect.

                          First, as I already stated above (and as supported by the JLS section I quoted and the code I posted), we'd get exactly the same behavior even if Collection did not declare those methods.

                          Second, whether those methods are there or not, we still have to compare each element individually. The fact that's this is done inside the method doesn't change that, and this is the case regardless of whether Collection declares those methods or not.
                          • 10. Re: why there are equal and hashcode methods are there in collection ?
                            EJP
                            833545 wrote:
                            Jverd i agree with your explanation, but ...
                            But nothing. Either you agree or you don't. The part after 'but' just shows you don't even understand the explanation, let alone agreeing with it.