473,387 Members | 1,374 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.

hashCode()

madhoriya22
252 100+
Hi,
I have wrapped to strings in a object using hashCode of these strings.......Now I want to know how can I get back these strings from the object...............I am putting this object as a key in a HashMap.....For more details kindly look here
Jul 12 '07 #1
11 2291
r035198x
13,262 8TB
Hi,
I have wrapped to strings in a object using hashCode of these strings.......Now I want to know how can I get back these strings from the object...............I am putting this object as a key in a HashMap.....For more details kindly this <a href = 'http://www.thescripts.com/forum/thread675010.html'>post</a>
Why (and how) did you use hashCode to make objects out of the Strings?
Maybe if you post your code ...
Jul 12 '07 #2
madhoriya22
252 100+
Why (and how) did you use hashCode to make objects out of the Strings?
Maybe if you post your code ...

If you will look at that hyperlink.......then you will get what I am trying to say here......by d way code is like:-
Expand|Select|Wrap|Line Numbers
  1. class WorkPackageMapKey {
  2.   private String name;
  3.   private String status;
  4.   // constructors, getters, and setters as usual
  5.   // for the above properties.
  6.  
  7.   // the following methods override those
  8.   // in Object and are recommended to make a
  9.   // logically correct "key" behavior. the exact
  10.   // details are up to you, but equals and hashcode
  11.   // must be consistent.
  12.  
  13.   public boolean equals(Object o) {
  14.     if (this == o) return true;
  15.     if ( ! (o instanceof WorkPackageMapKey)) {
  16.       return false;
  17.     }
  18.     WorkPackageMapKey wpmk = (WorkPackageMapKey)o;
  19.     if ( ! this.name.equals(wpmk.name)) {
  20.       return false;
  21.     }
  22.     if ( ! this.status.equals(wpmk.status)) {
  23.       return false;
  24.     }
  25.     //etc
  26.     return true;
  27.   }
  28.  
  29.   // there's a PDF Sun made available to correctly account
  30.   // for primitive types, but if you don't have those...
  31.  
  32.   public int hashCode() {
  33.     int hashcode = name.hashCode();
  34.     hashcode += status.hashCode();
  35.     //etc
  36.     return hashcode;
  37.   }
  38. }
  39.  
  40.  
Jul 12 '07 #3
r035198x
13,262 8TB
If you will look at that hyperlink.......then you will get what I am trying to say here......by d way code is like:-
I saw it. Look at Jos's example again. Where's your constructor that takes the two strings?
If you want to get each string individually, just provide getters for them. That has nothing to do with hashCode.
Jul 12 '07 #4
madhoriya22
252 100+
I saw it. Look at Jos's example again. Where's your constructor that takes the two strings?
If you want to get each string individually, just provide getters for them. That has nothing to do with hashCode.

If that has nothing to do with hashCode..........then why we are implementing equals() and hashCode() method?......If simply I have to wrap it in a object then I have Value Object(VO) class........I can do it with that also......Plz correct me if I am wrong...............
Jul 12 '07 #5
r035198x
13,262 8TB
If that has nothing to do with hashCode..........then why we are implementing equals() and hashCode() method?......If simply I have to wrap it in a object then I have Value Object(VO) class........I can do it with that also......Plz correct me if I am wrong...............
I didn't say equals and hashCode are not important. You should include them always but they are not used for solving the question you asked in this thread. Read this thread again from the start.
Jul 12 '07 #6
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.   public int hashCode() {
  2.     int hashcode = name.hashCode();
  3.     hashcode += status.hashCode();
  4.     //etc
  5.     return hashcode;
  6.   }
  7.  
Not that it matters much, but a slightly better hashCode method is this:

Expand|Select|Wrap|Line Numbers
  1.   public int hashCode() {
  2.     return 3*name.hashCode()+status.hashCode();
  3.   }
  4.  
It gives different hash values for (a,b) and (b,a) while your version returns
identical values for those pairs of strings.

kind regards,

Jos
Jul 12 '07 #7
r035198x
13,262 8TB
Not that it matters much, but a slightly better hashCode method is this:

Expand|Select|Wrap|Line Numbers
  1.   public int hashCode() {
  2.     return 3*name.hashCode()+status.hashCode();
  3.   }
  4.  
It gives different hash values for (a,b) and (b,a) while your version returns
identical values for those pairs of strings.

kind regards,

Jos
Something which the OP may need to think about. Do they want key (a, b) to map to the same value mapped to by key (b, a) ?
Jul 12 '07 #8
madhoriya22
252 100+
Something which the OP may need to think about. Do they want key (a, b) to map to the same value mapped to by key (b, a) ?

Actually my key value mapping will be like this:-
Expand|Select|Wrap|Line Numbers
  1. 1.  K(a1,b1) ---->  v1
  2. 2.  .............
  3. 4.  K(a1,b4) ---->  v4
  4.  
  5. 5.  K(a2,b1) ---->  v5
  6. 6.  .............
  7. 8.  K(a2,b4) ---->  v8
  8.  
  9. 9.  K(a3,b1) ---->  v9
  10. 10.....................so on.....
  11.  
Values(number) can be same or different for all keys..........so I dont think... here is any case of key(a,b) or key(b,a) mapping to same value
Jul 12 '07 #9
JosAH
11,448 Expert 8TB
Actually my key value mapping will be like this:-
Expand|Select|Wrap|Line Numbers
  1. 1.  K(a1,b1) ---->  v1
  2. 2.  .............
  3. 4.  K(a1,b4) ---->  v4
  4.  
  5. 5.  K(a2,b1) ---->  v5
  6. 6.  .............
  7. 8.  K(a2,b4) ---->  v8
  8.  
  9. 9.  K(a3,b1) ---->  v9
  10. 10.....................so on.....
  11.  
Values(number) can be same or different for all keys..........so I dont think... here is any case of key(a,b) or key(b,a) mapping to same value
Is your map a full map, i.e. for keys a1 ... a4 and b1 ... b4 are there 16 compound
keys in your map?

kind regards,

Jos
Jul 12 '07 #10
madhoriya22
252 100+
Is your map a full map, i.e. for keys a1 ... a4 and b1 ... b4 are there 16 compound
keys in your map?

kind regards,

Jos

Ya it is a full map........cant say that there will be exact 16 compound keys..........but key value combinations will be like that only(last reply)...
Jul 12 '07 #11
JosAH
11,448 Expert 8TB
Ya it is a full map........cant say that there will be exact 16 compound keys..........but key value combinations will be like that only(last reply)...
So for n and m keys you always have n*m values in your map? If so you have
a Cartesian product and you can handle that with two simple maps and a two
dimensional array of values.

kind regards,

Jos
Jul 12 '07 #12

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

Similar topics

1
by: pentium77 | last post by:
Hi, Just wondering what's the algo used to compute hashcode in Java ? Does anybody here know ? Thanks, -MK.
4
by: Nick | last post by:
Hi all, I am using GetHashCode on unique strings to get a unique integer for a string that I can then place into a database (use int rather than the string to make indexing faster). The problem...
3
by: Josema | last post by:
Hi to all, In a lot of type of classes in the .net framework classes library i see that has the method GethashCode. What is the HashCode of anything, and in wich kind of functionality are used...
1
by: Frank | last post by:
Hi, I save the hashcode of a datatablerow. How do I use that hascode to get to the original datarow? Thanks Frank
8
by: MuZZy | last post by:
Hi, Why for god sake they change implementation of String.GetHashCode() from ..NET 1 to .NET 2? We were storing some user passwords in hashcode, now we can't upgrade those clients with .NET 2...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
1
by: Ing. Davide Piras | last post by:
Hi there, I really don't know if I can post here or microsoft.public.dotnet.framework.adonet could be better... anyway: in my .NET 2.0 C# arcitecture (windows form as client application, web...
1
by: praveenkumarvpk | last post by:
public static void main(String args) { String elements = { "A", "B", "C", "D", "E" }; Integer l = {1,2,68}; Integer y=1; int d=0; for(int i=0;i<l.length;i++) ...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.