File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is this not running as expected?

 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a little modification in your hashcode method is needed....
return this.employeeID.hashcode();
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this won't compile. employeeId is an int. And I tried this.hashcode(). It's giving the same unexpected result.
 
Jibitesh Prasad
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me what does the

is.. it hasn't been declared.
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't overridden the method hashCode()

In your code it's hashcode()

I hope this helps.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Remko, that did help. Such a silly mistake, so stupid of me. Thanks a lot man. I deserve this......
 
Jibitesh Prasad
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it with hashCode().. still i get the error
int cannot be dereferenced

Anyone help me with this.
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your code from

return this.employeeId.hashcode();

to

return this.employeeId;

And your error will be solved.
 
Jibitesh Prasad
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but man in all the tutorials i have been through, they return a hashCode()..
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really?? I don't much about annotations in Java. Thanks. It would actually prove very useful.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic