4

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

asked Apr 28, 2016 at 15:11
3
  • 1
    Did you build the 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. Commented Apr 28, 2016 at 15:24
  • As @JimGarrison said, you should reconsider your data structure. Commented Apr 28, 2016 at 15:51
  • Hi, thanks to all for your quick responses. Actually I didn't build this structure completely by myself. However I understand your comments about creating a class, and this probably will be my next step as I am currently learning about the objects phylosophy Commented Apr 29, 2016 at 9:22

3 Answers 3

2

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

answered Apr 28, 2016 at 15:50

1 Comment

Thanks for your response Hitesh. I solved it by using the values instead of keySet. However your response was useful to me in order to get the values.
1

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.

OneCricketeer
193k20 gold badges145 silver badges276 bronze badges
answered Apr 28, 2016 at 15:30

Comments

1

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!!

answered Apr 29, 2016 at 9:27

2 Comments

This is very fragile since order is not guaranteed in Hashtable. Instead of accessing the values list by copying the collection returned by 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.
Thanks Valentin, it works better than my solution. I also agree with you in relation with Manu AG's solution.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.