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

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

Sign up to request clarification or add additional context in comments.

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.

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

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

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.