File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes hashcode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashcode()" Watch "hashcode()" New topic
Author

hashcode()

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi FolkS,

I was just developing a small program .as seen below....



Now my query is that in hashcode() method we are writing name.hashcode() that means we are taking out the integer value of name...? if yes then why don't of salary also...?? and second query is that for equals() we are wriring this.name.equals(p.name) but for salary we are using directly == operator..!! it is because in java we can use primitive operators directly..!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16

Because salary is an int. Ofcourse you cannot call methods such as equals and hashCode on an int. Primitive types don't have methods.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Jesper de Jong wrote:Because salary is an int. Ofcourse you cannot call methods such as equals and hashCode on an int. Primitive types don't have methods.


so does it mean name and job are string thats why we can call methods such as hashcode not an integer ...!! please explain..!!!
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

and what about if I comment out the hashcode method particularly and making a separate hashset class of it...for example in the below code..



In that case it will not use the hashing functionality while storeing the objects in bucket..!! please explain..!!!

As I have crosschecked by commenting the hashcode method it gives me the following result ...


19770577 Saral Trainer 34000
17008065 Ravi Administrator 44000
16197143 Sachin Team Leader 54000
18426253 Anupam Programmer 34000
3195425 Priyanka Manager 54000
4354460 Sachin Programmer 24000
28117098 Saral Trainer 34000



as seen above Saral is taken as twice ..and with different hashcodes , since it is taking the hashcode of Object class...!!please advise..!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Saral Saxena wrote:and what about if I comment out the hashcode method particularly and making a separate hashset class of it...


That sentence doesn't make any sense. You removed the hashCode() method from the class, but you didn't "make a separate hashset class" of anything. You just created a HashSet. That has nothing to do with your Emp class at all.

Since your Emp class now doesn't have a hashCode() method, it follows that all Emp objects will be treated as being different. Even if you think that two Emp objects are the same because they contain the same data, they are still treated as being different. That's because you don't have a hashCode() and an equals() method which tells classes like HashSet when to treat two Emp objects as "equal".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Saral Saxena wrote:as seen above Saral is taken as twice ..and with different hashcodes , since it is taking the hashcode of Object class...!!please advise..!!


Of course. Your Emp class doesn't have a hashCode() method -- remember that you commented it out? -- and so calling its hashCode() method results in the non-overridden Object method being called.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14103
    
  16

Saral Saxena wrote:so does it mean name and job are string thats why we can call methods such as hashcode not an integer ...!! please explain..!!!

name and job are String objects. Class String has hashCode() and equals() methods, so you can call those. (In fact, all classes have hashCode() and equals() methods because they are defined in class Object, and all classes extend Object). Primitive types such as int are not objects, so you cannot call methods on them.

The contract for hashCode() and equals() is as follows: If two objects are equal according to the equals() method, then their hash codes must be the same; for both objects, hashCode() must return the same value.

Note that this only works one way. If two objects are not equal, they can still have the same hash code. The only requirement is that if they are equal, they must have the same hash code.

If you implement only the equals() method and not the hashCode() method, then you violate the contract, because your class will inherit the hashCode() method from class Object. That implementation will return different values if you have two Emp objects which have the same name, job and salary.

If you try to store objects that violate the hashCode / equals contract in a hash-based collection such as HashSet, the results are undefined. You might see the object twice in the set, or just once, or maybe not at all. It depends on the exact implementation of HashSet.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Paul Clapham wrote:
Saral Saxena wrote:and what about if I comment out the hashcode method particularly and making a separate hashset class of it...


That sentence doesn't make any sense. You removed the hashCode() method from the class, but you didn't "make a separate hashset class" of anything. You just created a HashSet. That has nothing to do with your Emp class at all.

Since your Emp class now doesn't have a hashCode() method, it follows that all Emp objects will be treated as being different. Even if you think that two Emp objects are the same because they contain the same data, they are still treated as being different. That's because you don't have a hashCode() and an equals() method which tells classes like HashSet when to treat two Emp objects as "equal".


Thanks Paul.I got that in this case the default behavior of Object class hashcode() method will be used...!! thanks a lot for clearing that...!! could you also please specify the role of Equals method() that will make understandings more clearer..!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saral Saxena wrote:could you also please specify the role of Equals method() that will make understandings more clearer..!!


If you don't know what equals() is for, you should SearchFirst.(⇐ click)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saral Saxena wrote:
Can you please provide the examples ..!! that will help to understand better...!! thanks a lot..!!


SearchFirst.(⇐ click)

As I keep telling you, over and over, sites like this are not a substitute for spending the time and effort to do your own research, reading, studying, coding, testing, thinking, and generally trying to come to an understanding on your own. Only after you do all that and are still stuck should you post a question.

And when you do post, it should not be vague, such as, "provide an example." What do you want an example of? What examples did you find and what about them did you not undrestand?

So, to summarize, 3 things that you must do that you are not doing:
ShowSomeEffort(⇐ click)
SearchFirst(⇐ click)
TellTheDetails(⇐ click)

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
And while we are on about it, you do realise your equals() method fails to maintain the general contract for equals() and is written incorrectly?
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Campbell Ritchie wrote:And while we are on about it, you do realise your equals() method fails to maintain the general contract for equals() and is written incorrectly?


Hi Campbell,

please guide me what is wrong with my equals method...!! so that Help me to understand more...thanks in advance..!!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Saral Saxena wrote:please guide me what is wrong with my equals method...!! so that Help me to understand more...thanks in advance..!!

If you would check the Javadoc of java.lang.Object, you'd see that the equals method should return false if the argument is null or belongs to a class that cannot be cast to Emp. In your implementation, you would get a ClassCastException on line 17 if the argument cannot be cast to Emp (e.g. String), or a NullPointerException on line 18 if the argument is null.

There are two ways of writing equals methods that are accepted by large groups:

There is not a definite answer on which of these two is correct.

Using instanceof may break the symmetry rule of Object.equals. Suppose you have classes Point and ColoredPoint, where ColoredPoint extends Point and adds an extra color field. Both Point and ColoredPoint use instanceof inside equals. Suppose you have a Point p and a ColoredPoint cp. Both have the exact same values for the fields defined by Point. Now p.equals(cp) will be true, as cp is a Point with the same values. However, cp.equals(p) will be false because p isn't a ColoredPoint.

Using the getClass() comparison solves this issue, but it can break the Liskov Substitution Principle.

I usually prefer the getClass() comparison though, as the Object.equals contract is more important to me.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
There are other ways to write equals without using multiple return, but you must be careful to use the && operator not & . . . and, avoiding the nasty multiple castsYou will realise the this. is redundant, but improves clarity, and also that some test can be taken in a different order. The test for nullity must precede the equality of classes test, and that must precede the cast.

As a design principle, it would be better to make sure none of the fields can be null, by applying tests to the constructor and the setXXX methods, and you can then dispense with the ?: operators. The () are not an intrinsic part of the ?: operator, but are here necessary to maintain the ?: as a unit because it has a low precedence.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

With Java 7 you can use java.util.Objects and its equals(Object,Object) method for that.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Rob Spoor wrote:With Java 7 you can use java.util.Objects and its equals(Object,Object) method for that.


Hi Rob,

Thanks a lot , could you please show a sma;ll program h, how java 7 hase changed it, that would make understanding more clear..!! thanks a lot....!










1
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saral Saxena wrote:
Rob Spoor wrote:With Java 7 you can use java.util.Objects and its equals(Object,Object) method for that.


Hi Rob,

Thanks a lot , could you please show a sma;ll program h, how java 7 hase changed it, that would make understanding more clear..!! thanks a lot....!


I have a better idea. Why don't you read up on that class and that method, write some code that uses it, then go back and try to figure out how it might be applied to the situation above.

Then, after you do that, if you still can't figure it out, post here with details about what you tried and what specifically you don't understand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode()