• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashcodes

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is an eg from K&b quiz..here after running the code i get same answer even if i keep hashcode method or delete hashcode method...but the answer given there is that if hashcode method is overridden then duplicate values do not get printed and my answer should be 3 instead of 4???
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meghs karve wrote:...but the answer given there is that if hashcode method is overridden then duplicate values do not get printed and my answer should be 3 instead of 4???


Yes thats right, you're adding the same element having value "34" 2 times, but as it IS A Set, the object didn't accept the second element with value 34.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
copy my code as it is and run and c the output.....u will understand what i am trying to say...............


thanks ..............
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace this


with this

and run the code, I hope you will get your answer.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one is very tricky. It should be:

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means i cant sort and remove duplicates in case if i put a Class in generics....It will only work for wrapper class objects???not user defined classes???
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meghs karve wrote:that means i cant sort and remove duplicates in case if i put a Class in generics....It will only work for wrapper class objects???not user defined classes???


Just ignore what Neha Daga said. Of course you can use collections classed with your own classes. Just implement the hashCode and equals methods correctly.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya right....i expected to get an output of 3 but instead getting 4...strange....still cant figure out why....
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I meant to say is you are creatint a new object everytime doesn't matter what it contains is equal or not.

@Wouter Oet
I wanted him to figure out his mistake
If he had realised what I wrote above, He would have been able to figure out what was wrong in his hashCode method.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:ya right....i expected to get an output of 3 but instead getting 4...strange....still cant figure out why....


Have you read the topic? I just posted the solution
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set cant take equal objects....one is a duplicate....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh didnt see the small c
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:what I meant to say is you are creatint a new object everytime doesn't matter what it contains is equal or not.


NO it does matter what is contains. The equals method should return true is 2 objects are meaningful equal.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have edited my message please read.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ignore what Wouter has said....add method will give false if it sees that if two objects are meaningful equal
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your intentions where good although I find the advice vague. Maybe it's the language barrier.

My advice: use the @Override annotation. Then you'll get an compile-time error when your
not overriding something. What just happened.

Raju Champaklal wrote:ignore what Wouter has said....add method will give false if it sees that if two objects are meaningful equal


But in the add method hashCode and equals are used. So if you don't implement the hashCode and equals methods properly the method
will return true even though the set already contains an meaningful equal object.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ignore what Wouter has said....dont use the @Override annotation...dont use netbeans...or eclipse...now you know that there is a capital C in the hashCode method
just use simple editors like editplus or notepad
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
meaningful equal objects mean the equals and the haashCode method has been overridden.....
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use the override annotation? It's part of the java language! I agree with you that you shouldn't use ide's. I write all my code in gedit.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:meaningful equal objects mean the equals and the haashCode method has been overridden.....


So if i have new Integer(5) and new Integer(6) they are meaningful equal because the hashCode and the equals methods have been overriden??
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want to study for scjp dont use IDEs....but if you are working on some project definitely go for IDEs....they make your work faster...but if you dont understand why it is getting faster using an IDE wont help
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
meaningful equals object mean new Integer(5) and new Integer(5).....
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree but you haven't answered my question. Why not use the java.lang.Override annotation?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:meaningful equals object mean new Integer(5) and new Integer(5).....


Yes but then is not what you said here:

Raju Champaklal wrote:meaningful equal objects mean the equals and the haashCode method has been overridden.....

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whatever i mean here is only while preparing for scjp ....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Turtle(8) and new Turtle(8) are not meaningful equal if you dont override equals and hashCode.....two object are equal if they are the same...two objects are meaningful equal if they are not the same object but equal according to the hashCode and equals method that have been overridden

i hope you are clear now
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That I understand. That is why I'm here. I'm currently preparing for SCJP. However you have still not giving me a reasoned explanation about why not to use the java.lang.Override annotation.
It's part of the java language. It's not bound to a certain compiler or IDE.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look if you use that notation then the compiler will help you find the error....this is good if you are building a project and you want to find the trivial errors fast....then i totally agree use that notation....but while preparing for scjp i think the better you get at finding out the errors the better...if i hadnt seen this question today i wouldnt have ever found that c in hashCode is capital...now in future i would make sure that C is capital....
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then we agree to disagree. I would use the annotation even while preparing for SCJP. The compiler error that you can't override hashcode (no capital c) sticks with you .
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was just giving a suggestion....and please never ask someone to ignore someone else's advice...
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe that was not very nice. However then you shouldn't do it too. His post was not very helping.
Instead of helping to solve the problem he suggest a alternative which is in most cases not the best answer and certainly not in this case.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because i was making you taste your own medicine...it doesnt look nice....sorry..
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Wouter Oet

I am not a he I am a She.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouter
neha daga is a she ...
She has become red already.....hhehehehe with anger
well guys i am just joking hope all people will be cool..
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really sorry
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its fine man...no hard feelings
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be nice everyone

Raju Champaklal wrote:look if you use that notation then the compiler will help you find the error....this is good if you are building a project and you want to find the trivial errors fast....then i totally agree use that notation....but while preparing for scjp i think the better you get at finding out the errors the better...if i hadnt seen this question today i wouldnt have ever found that c in hashCode is capital...now in future i would make sure that C is capital....



The @Override is there for a reason:

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.



It is highly recommended that the @Override annotation be used when you are overriding the declaration or implementing the method from an interface (java 6).

In the example, being discussed here, the intention is to override the implementation of hashCode(). Irrespective of whether the example is for SCJP or for any project, it is right to use the @Override.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic