Reputation: 55
I have a text file like the format below
module1.q1=a1
module1.q2=a2
module2.q1=a1
module2.q5=a6
..
..
I have a class objectsHmp which has two String variables to store questions(q1, q2, q3,...) and answers(a1,a2,a3,..). what i'm trying to do is keeping the module id as the key, i want to populate a hashmap with values as arraylist of objects(of objectsHmp). And in turn i'm storing the keys in an arraylist of String.
In the below code snippet,
*l_ext_keys* is an arraylist of String which stores the keys,
*l_extract* is a HashMap>,
*l_temp_array* is an arraylist of object objectsHmp,
while((line=br.readLine()) != null)
{
String[] ss = new String[2];
ss = line.split("=");
String tss = ss[0];
String[] kss = tss.split("\\.");
objectsHmp temp = new objectsHmp(kss[1],ss[1]);
if(!prev.equals(kss[0]))
{
l_ext_keys.add(prev);
l_extract.put(prev,l_temp_array);
l_temp_array.clear();
}
l_temp_array.add(temp);
prev = kss[0];
}
l_extract.put(prev,l_temp_array);
l_ext_keys.add(prev);
l_temp_array.clear();
Problems
The Hashmap is getting populated but it's not having values for some keys present in the l_ext_keys. I tried printing the length of the arraylist and hasmap, but its having a great difference(arraylist with the keys having more values than hashmap).
One possible reason for this difference is some module is repeated. I dint override any function
Question What are the possibilities that hashmap couldn't get values for the keys present in the list? Or am I doing some big mistake here?
Upvotes: 2
Views: 1537
Reputation: 1177
Its not possible to have a key more than once in a HashMap
. Keys must be unique. So you override the old key with your approach right now.
You could use the HashMap as a directory, saving all values for one key in an Array or ArrayList and then define:
HashMap<String, ArrayList<String>> directory = new HashMap<String, ArrayList<String>>();
After that you can retrieve an ArrayList holding the values for q1 with the key q1
EDIT: Your code is missing some information, but as far as I can see you are putting module1 and so on as the key in your hashmap. You check through if(!prev.equals..) for a new module, but that means only the first occurence of some module is being stored (if your file is sorted). If the list is not sorted, its getting overriden somewhere in the process.
Your whole function seems a bit buggy. :) Also check the code where you work with the temp arrayList, because you always add the question and answer to it. So some questions&answers get even saved with the wrong module. And at the first run of the while loop l_temp_array seems to be empty, so you add module1 with an empty value to the HashMap
..
Upvotes: 3
Reputation: 4623
The problem with your code is that it doesn't handle well cases when the module names are not sorted, e.g.
module1.q1=a1
module2.q1=a1
module1.q2=a2
module2.q5=a6
You can add values directly to the hashmap without using temporary lists, like this:
objectsHmp temp = new objectsHmp(kss[1],ss[1]);
ArrayList<objectsHmp> list = l_extract.get(kss[0]);
if(list == null){
list = new ArrayList<objectsHmp>();
l_extract.put(kss[0], list);
}
list.add(temp);
This solution will remove the risks I mentioned in the beginning.
Also, you're not following 100% the Java notation conventions, e.g. method names should have capital first letter; and for variable names the camelCase notation is preferred rather than underscores.
Upvotes: 0