I have a function that is capable to read data from multiple files with the following structure:
id date temp psal
1 2016年02月01日 37.6 35.9
2 2016年02月02日 30.3 35.7
3 2016年02月03日 28.2 36.8
4 2016年02月04日 27.7 37.7
5 2016年02月05日 28.7 37.9
6 2016年02月06日 28.7 37.9
The function is:
ArrayList<Hashtable<String, ArrayList<String>>> processedFilesArray = ReadFiles(files);
If I try to retrieve the data by using for example:
System.out.println(processedFilesArray.get(0));
Then I get the following response:
{Psals=[35.9, 35.7, 36.8, 37.7, 37.9, 37.9], Temps=[37.6, 30.3, 28.2, 27.7, 28.7, 28.7], ids=[1, 2, 3, 4, 5, 6], DateStrings=[2016年02月01日, 2016年02月02日, 2016年02月03日, 2016年02月04日, 2016年02月05日, 2016年02月06日]}
My question is: How can I obtain the different values of the keys (Temps, Psals, ids, etc) by sepparate to plot them using jfreechart?
Thanks in advance
3 Answers 3
You can get list of keys in your Hashtable by method keySet(), which return Set of Keys.
You can convert Set to Array list like this :-
ArrayList keyList = new ArrayList(processedFilesArray.get(0).keySet());
More about keySet http://www.tutorialspoint.com/java/util/hashmap_keyset.htm
1 Comment
You can obtain the keys and values by this way:
//Each hastTable of proccesedFilesArray
for(Hashtable<String,ArrayList<String>> processedFile : processedFilesArray){
//Each entrySet of proccesedFile
for(Entry<String, ArrayList<String>> entry : processedFile.entrySet()){
//Filter here by key or value with if conditions
String key = entry.getKey();
ArrayList<String> listValue = entry.getValue();//List of Strings
for(String value : listValue){
//each value per hashtable key
}
}
}
I dont really know how do you want to work with this but that could be a way to iterate your list and get the values of each record.
Comments
I finally solved this issue following Hitesh response, by using:
ArrayList keyList = new ArrayList(processedFilesArray.get(1).values());
System.out.println(keyList.get(0));
This returns me the first group (Psals) from the second file read:
[35.9, 35.7, 36.8, 37.7, 37.9, 37.9]
Thank you guys for your help!!
2 Comments
Hashtable.values()
in an ArrayList
and accessing these by index, you should prefer to access it this way: processedFilesArray.get(i).get("Psals")
. Ideally, if you want to process all the elements in the Hashtable
, you should use Manu AG's answer.
ArrayList<Hashtable<String, ArrayList<String>>>
yourself? If so, it's the wrong data structure (effectively a "parallel arrays" structure). You should define a class to hold one line of input as an object.