wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why is this not running as expected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is this not running as expected?" Watch "Why is this not running as expected?" New topic
Author

Why is this not running as expected?

Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
This is the code for my class:

package SCJP;
import java.util.HashMap;
import java.util.ArrayList;
class EqualsTest
{
public static void main(String[] args)
{
IBM c1 = new IBM(127331);
IBM c2 = new IBM(127314);
IBM c3 = new IBM(127331);
IBM c4 = new IBM(127314);
HashMap hm = new HashMap();
ArrayList ar = new ArrayList();
hm.put(c1,"Sidd");
hm.put(c2,"Ket");
ar.add(c1);
ar.add(c2);
checkEquality(c4,c2);
retrieveName(hm,ar);

}
private static void checkEquality(IBM one, IBM two)
{
if(one==two){
System.out.println("one == two");
}else{
System.out.println("one != two");
}
if(one.equals(two)){
System.out.println("one equals two");
}else{
System.out.println("one not equals two");
}
}
private static void retrieveName(HashMap hm, ArrayList hs)
{
IBM c = new IBM(id);
System.out.println(hm.get(c));
System.out.println(hs.contains(c));
}
}

class IBM
{
private int employeeId;
IBM(int employeeId){
this.employeeId = employeeId;
}
public int getEmployeeId()
{
return this.employeeId;
}
public boolean equals(Object o)
{
if(o instanceof IBM){
IBM c = (IBM)o;
if(this.getEmployeeId() == c.getEmployeeId()){
return true;
}else{
return false;
}
}else{
throw new ClassCastException();
}

}
public int hashcode()
{
return this.employeeId;
}
};



// This program gives the following output:
one != two
one equals two
null
true

Why am I not able to fetch the name from HashMap when I have overridden the equals() method in my class?? Also, the result for ArrayList is true, which is what i expected. But when i use HashSet instead of ArrayList, the result is false. Why?? I am using Java 1.4.
Jibitesh Prasad
Greenhorn

Joined: Feb 20, 2007
Posts: 20
a little modification in your hashcode method is needed....
return this.employeeID.hashcode();
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
But this won't compile. employeeId is an int. And I tried this.hashcode(). It's giving the same unexpected result.
Jibitesh Prasad
Greenhorn

Joined: Feb 20, 2007
Posts: 20
Can you tell me what does the

is.. it hasn't been declared.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
You haven't overridden the method hashCode()

In your code it's hashcode()

I hope this helps.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
Sorry for that mistake, one has to pass an id there. If i pass 127331 as an Id, it should make an object correspondingly. And when i use it as a search key for the hashmap, then it should fetch the value based on the equals method.
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
Thanks Remko, that did help. Such a silly mistake, so stupid of me. Thanks a lot man. I deserve this......
Jibitesh Prasad
Greenhorn

Joined: Feb 20, 2007
Posts: 20
i tried it with hashCode().. still i get the error
int cannot be dereferenced

Anyone help me with this.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Change your code from

return this.employeeId.hashcode();

to

return this.employeeId;

And your error will be solved.
Jibitesh Prasad
Greenhorn

Joined: Feb 20, 2007
Posts: 20
but man in all the tutorials i have been through, they return a hashCode()..
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
One needs to understand the basic requirement for hashcode of an object. Its useful when one puts the objects in a collection, especially those collections which do not implement indexing, like Maps etc.
A hashCode() method can return any number, or even a constant. It would just affect the performance of the collections, nothing else. But without overriding this method, one cannot search a collection.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Originally posted by Sidd Cool:
Thanks Remko, that did help. Such a silly mistake, so stupid of me. Thanks a lot man. I deserve this......

The @Override annotation exists exactly to help you prevent this mistake. Adding the @Override annotation to a method tells the compiler that you mean to override a method in a superclass. If you would have done this:

Then you would have gotten a compiler error - because hashcode() (with lower-case c) does not override a method in a superclass.


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

Joined: Feb 20, 2007
Posts: 152
It won't give a compiler error, because there is already a hashCode() method in the class Object. When the VM doesn't find it in the sub class, it fetches the superclass method. But that method returns a unique hashcode for each object, which is not at useful to us, as, in case of searching keys in a collection(e.g. HashMap), the hashcodes of two objects have to be equal, else it won't search the object.
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
It won't give a compiler error, because there is already a hashCode() method in the class Object. When the VM doesn't find it in the sub class, it fetches the superclass method. But that method returns a unique hashcode for each object, which is not at useful to us, as, in case of searching keys in a collection(e.g. HashMap), the hashcodes of two objects have to be equal, else it won't search the object.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Sidd, were you replying to previous post? If yes, then read the last line of my previous answer again carefully:

"Then you would have gotten a compiler error - because hashcode() (with lower-case c) does not override a method in a superclass."

The @Override annotation is meant to protect you from mistakes such as spelling the name of a method that you intend to override wrong.
[ February 23, 2007: Message edited by: Jesper Young ]
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
Really?? I don't much about annotations in Java. Thanks. It would actually prove very useful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why is this not running as expected?