• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to define hashCode in java?

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all Ranchers,
There is java file and according to output I want to know what is the base of hashCode. I do not know exact about what does hashCode() method do.



The out is : -

Thanks in advanced!!.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rd Dari wrote: I do not know exact about what does hashCode() method do.

http://en.wikipedia.org/wiki/Java_hashCode()
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is better to read the documentation about the hashCode method first.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yogesh Gnanapraksam wrote:It is better to read the documentation about the hashCode method first.

Might as well link to the current documentation: java.lang.Object#hashCode() (although I doubt it's changed).
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya you all are right.

I must read the hashCode() method to understand it clearly.


Thanks a lot.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth are you using Enumeration and Vector, which are regarded as outmoded? Use Iterator and ArrayList instead.
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it was different post by mistake I mixed it in hashCode() method
Sorry!!!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it was an interview question, why didn’t you say so? Those questions are completely unrelated to real life. You can’t say what the capacity of a Vector it, unless you have seen all its method calls. Even then you might not know it. You are better off going on about not knowing implementation details, and how you don’t worry about capacity because you know it changes to accommodate whatever you put in it. Also say you usually use ArrayList, which might have a different mechanism for increasing capacity. Mentioning the ensureCapacity method and how it speeds up adding would be a good move.
 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I am asking about the output

enumeration days:
-1807319568
-1984635600
687309357
-897468618
1553582752
2112549247
-2049557543

how it is coming for program

Please reply it.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just printing the hash codes for those Strings. The documentation for java.lang.String#hashCode() tells you how that's calculated.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It’s printing the hash codes in decimal. I might prefer to use… and print the hash codes in hexadecimal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic