views:

97

answers:

1

Hi ! I have defined a HashMap which uses a double type key and another HashMap as value as shown

HashMap<Double, HashMap<Double, String>> HM1 = new HashMap<Double, HashMap<Double, String>>();

Now for each entry of this HashMap I have a reference to a different HashMap; the name of which is derived from the key value of that entry in this HashMap. For example: If my key value in HM1 is 8, then the name of the HashMap to be referenced in "Alpha8". If the key value in HM1 is 6, then the name of the HashMap to be references in "Alpha6". So my syntax in adding these to the HashMap HM1 is HM1.put(8, Alpha8); and HM1.put(6,Alpha6);

My problem:

The key values are pre-defined which I am reading from a text file. Hence, I open the file, write a scanner object to pick each value and put it in a double type variable keyvalue. However, to get the value for this key, I defined a string s1 = "Alpha"+keyvalue.toString();

My main problem is how do I pass this string in my put function. Because if I say HM1.put(keyvalue, s1); it is the equivalent of passing a double key and a string value rather than a double key and the reference to another HashMap. For primitive data types, you may be able to wrap but for a HashMap reference, I'm not sure how to do it.

A: 

Assuming you have all the possible Alpha* references available to you... I will just put that logic in a simple if condition and do like this

if(keyvalue == 1) 
{
HM1.put(keyvalue, Alpha1)
} 
else(keyvalue == 2)
{

}
--
johnbk
Or, add the keyvalue to the hash, so you have: `HM1.put(anAlphaMap.get("keyvalue"), anAlphaMap);` and forget about all the if statements.
Tony Ennis
Thanks John but my problem is I almost have 350 different keys. Plus, these may increase later which I can otherwise update directly (if u use a hasNext() through my text file instead of getting into the code
Swami
Tony, could you please elaborate on your comment? If I am not mistaken, unless I have both keyvalue and Alpha mapped in a HashMap (which is where I'm struggling), I would not be able to get the keyvalue for a particular AlphaMap. Please elucidate. Would be really helpful
Swami
@Swami - If I understood Tony correctly what he was trying to say is to gather all the alpha* references available to you and put them in HM1 directly without reading the 'keyvalues' from the file, I would suggest the same to solve your problem, incase if you want only the keyvalue pairs that are there in your text file in HM1, you first build your HM1 with all the alpha* references available to you using @Tony's solution and remove all the entries later that are not there in your text file keyvalues. Hope I am being clear enough.
johnbk
@swami - You know each 'alpha' hash needs it's own keyvalue for the HM1 hash. So, put that keyvalue into the particular alpha hash. For example, if you read data for alpha hash #102, add to the alpha hash a key/value pair of "keyvalue" and 102.0. Now every alpha has what it needs to find itself in HM1. I don't know if this helps in your exact situation - I was commenting on the if/else's in @johnbk's solution.
Tony Ennis
@Tony/John: What I'm trying to code up may give you a little more context.The code will read a file in which each row contains a string, a value which is a key in HM1 and a value which is a key in the corresponding Alpha Map. So the code reads the keyvalue of HM1 in the file, goes to that entry in the HM1 hashmap, goes to the Alpha Hashmap referenced as the value for that key, goes to the row defined by the key for the AlphaMap (from text file) and enters the string as the value for that key.If I add the keyvalue as per Tony's solution, where could I update that string value?
Swami
@John: I understand that assumption, but am I correct in assuming that I would have to manually add HM1.put(8, Alpha8) 350 times in my code without using a loop?
Swami
@Arian/ @jacobm: Yes you're right. The formatting got messed up. I updated the post twice with a P.S. at the bottom of my post as well. Obviously, that was not clear enough from my side. Apologies. @bemace: I hope my previous reply clarified your question?
Swami
Code: try { sc2 = new Scanner(new File("Book1.txt")); } catch (FileNotFoundException fnfe) { System.err.println("Error. File not found"); } while (sc2.hasNext()) { Double keyslat = sc2.nextDouble(); //key of HM1 String valueslat= "Alpha"+keyslat.toString(); // name of the Alpha which needs to be referenced HM1.put(keyslat, valueslat); } So what is the correct put()? HashMaps are defined beforehand. So **valuelat** contains a name of a HashMap which already exists
Swami
P.S. Sorry for the previous terse reply. Was desperately trying to fit my reply within the allocated number of characters :). Thanks a lot for your support so far !
Swami