[英]how to get values from an ArrayList<Hashtable<String, ArrayList<String>>>
我有一個能夠從多個文件讀取數據的函數具有以下結構:
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
功能是:
ArrayList<Hashtable<String, ArrayList<String>>> processedFilesArray = ReadFiles(files);
如果我嘗試使用例如檢索數據:
System.out.println(processedFilesArray.get(0));
然后我得到以下回復:
{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]}
我的問題是:如何通過sepparate使用jfreechart繪制它們來獲取鍵的不同值(Temps,Psals,id等)?
提前致謝
您可以通過以下方式獲取鍵和值:
//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
}
}
}
我真的不知道你想如何使用它,但這可能是一種迭代列表並獲取每條記錄值的方法。
您可以通過方法keySet()獲取Hashtable中的鍵列表,該鍵返回Set of Keys。
您可以將Set轉換為Array列表,如下所示: -
ArrayList keyList = new ArrayList(processedFilesArray.get(0).keySet());
有關keySet的更多信息, 請訪問http://www.tutorialspoint.com/java/util/hashmap_keyset.htm
我終於通過使用以下方法解決了Hitesh響應后的問題:
ArrayList keyList = new ArrayList(processedFilesArray.get(1).values());
System.out.println(keyList.get(0));
這將返回第二個文件讀取的第一組(Psals):
[35.9, 35.7, 36.8, 37.7, 37.9, 37.9]
謝謝你們的幫助!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.