473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a little bit confuse about HASH CODE

dmjpro
2,476 2GB
this is my code .....

Expand|Select|Wrap|Line Numbers
  1. class Object1
  2. {
  3.   private int data;
  4.   public Object1(int d)
  5.   {
  6.     data = d;
  7.   }
  8.   public int hashCode()
  9.   {
  10.     return 0;
  11.   }
  12. }
  13.  
  14. class HashCodeTest
  15. {
  16.   public static void main(String args[])
  17.   {
  18.     Object1 o1 = new Object1(100);
  19.     Object1 o2 = new Object1(200);
  20.     System.out.println("The hash code for o1: " + o1.hashCode());
  21.     System.out.println("The hash code for o2: " + o2.hashCode());
  22.     System.out.println("o1=o2: " + o1.equals(o2));
  23.   } 
  24. }
on which basic the EQUALS method is implemented???

as my two objects the hash code is same ....but the EQUALS method returns false.

plz explain.

regards.
Apr 23 '07 #1
2 1527
JosAH
11,448 Expert 8TB
this is my code .....

Expand|Select|Wrap|Line Numbers
  1. class Object1
  2. {
  3.   private int data;
  4.   public Object1(int d)
  5.   {
  6.     data = d;
  7.   }
  8.   public int hashCode()
  9.   {
  10.     return 0;
  11.   }
  12. }
  13.  
  14. class HashCodeTest
  15. {
  16.   public static void main(String args[])
  17.   {
  18.     Object1 o1 = new Object1(100);
  19.     Object1 o2 = new Object1(200);
  20.     System.out.println("The hash code for o1: " + o1.hashCode());
  21.     System.out.println("The hash code for o2: " + o2.hashCode());
  22.     System.out.println("o1=o2: " + o1.equals(o2));
  23.   } 
  24. }
on which basic the EQUALS method is implemented???

as my two objects the hash code is same ....but the EQUALS method returns false.

plz explain.

regards.
You didn't override the equals() method so the Object.equals() method is used
here. Object.equals() uses reference equality, i.e. every two distinct instances
of objects are considered not equal. This implies that only this will result as true:
Expand|Select|Wrap|Line Numbers
  1. Object o= new Object();
  2. System.out.println(o.equals(o));
If objects are considered equal, then the hashCodes should be equal. The other
way around isn't true, e.g. if the hashCodes are equal for two objects then the
objects needn't be equal and if the hashCodes aren't equal then the two objects
certainly aren't equal.

The default implementations of equals() and hashCode() in class Object obey
to this rule. If you overrride them both the new versions should also obey to this
rule.

kind regards,

Jos
Apr 23 '07 #2
dmjpro
2,476 2GB
thanx for ur reply.

ok i see.
Apr 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
13
by: ababeel | last post by:
Hi I am using a calloc in a hash table program on an openvms system. The calloc is used to declare the hash table char **pHashes; pHashes = calloc(hash_size,sizeof(char *)); //hash_size = 101 ...
3
by: keithl | last post by:
Hi (again !) I posted a question yesterday just for info on hashrefs which I have read and it makes sense. Putting this into proactive though has now toally confused me ! I have an XML file...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
1
by: pavanponnapalli | last post by:
hi , I have got a code as under : sub dbcall { my %hash; my $i=0; print "<<<<<<<@_>>>>>>> \n"; foreach(@_)
1
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.