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 11 2291
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 ...
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:- -
class WorkPackageMapKey {
-
private String name;
-
private String status;
-
// constructors, getters, and setters as usual
-
// for the above properties.
-
-
// the following methods override those
-
// in Object and are recommended to make a
-
// logically correct "key" behavior. the exact
-
// details are up to you, but equals and hashcode
-
// must be consistent.
-
-
public boolean equals(Object o) {
-
if (this == o) return true;
-
if ( ! (o instanceof WorkPackageMapKey)) {
-
return false;
-
}
-
WorkPackageMapKey wpmk = (WorkPackageMapKey)o;
-
if ( ! this.name.equals(wpmk.name)) {
-
return false;
-
}
-
if ( ! this.status.equals(wpmk.status)) {
-
return false;
-
}
-
//etc
-
return true;
-
}
-
-
// there's a PDF Sun made available to correctly account
-
// for primitive types, but if you don't have those...
-
-
public int hashCode() {
-
int hashcode = name.hashCode();
-
hashcode += status.hashCode();
-
//etc
-
return hashcode;
-
}
-
}
-
-
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.
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...............
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.
-
public int hashCode() {
-
int hashcode = name.hashCode();
-
hashcode += status.hashCode();
-
//etc
-
return hashcode;
-
}
-
Not that it matters much, but a slightly better hashCode method is this: -
public int hashCode() {
-
return 3*name.hashCode()+status.hashCode();
-
}
-
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
Not that it matters much, but a slightly better hashCode method is this: -
public int hashCode() {
-
return 3*name.hashCode()+status.hashCode();
-
}
-
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) ?
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:- -
1. K(a1,b1) ----> v1
-
2. .............
-
4. K(a1,b4) ----> v4
-
-
5. K(a2,b1) ----> v5
-
6. .............
-
8. K(a2,b4) ----> v8
-
-
9. K(a3,b1) ----> v9
-
10.....................so on.....
-
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
Actually my key value mapping will be like this:- -
1. K(a1,b1) ----> v1
-
2. .............
-
4. K(a1,b4) ----> v4
-
-
5. K(a2,b1) ----> v5
-
6. .............
-
8. K(a2,b4) ----> v8
-
-
9. K(a3,b1) ----> v9
-
10.....................so on.....
-
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
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)...
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: pentium77 |
last post by:
Hi,
Just wondering what's the algo used to compute hashcode in Java ? Does
anybody here know ?
Thanks,
-MK.
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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++)
...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |