2016-10-19 54 views
-1

从我的硬盘驱动器读取csv文件,并试图建立国家的嵌套HashMap和基于世界银行不同的度量键/值的内部HashMap的过程中被覆盖。外层地图似乎工作正常,但内层地图在将所有外键替换为最后一个国家/地区的数据时存在问题。这里是我的代码:Java的嵌套包含HashMap内的HashMap一个ReadFile的

public static HashMap<String, HashMap<String, Float>> loadPoliticalStabilityFromCSV(PApplet p, String filename){ 
    // HashMap key: country ID and data: some political stability measure 
    HashMap<String, HashMap<String, Float>> countryDataMap = new HashMap<>(); 
    HashMap<String, Float> metricsMap = new HashMap<>(); 

    // pull in the lines from the csv file 
    String[] rows = p.loadStrings(filename); 
    // Reads country name from CSV row 
    for (String row : rows){    

     // split row by commas not in quotations      
     String[] columns = row.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); 
     // Grab Data Point 
     for (int i = columns.length -1; i > 3; i--){ 
      if(!columns[i].equals(null)){ 
       metricsMap.put(columns[2], Float.parseFloat(columns[i])); 
       countryDataMap.put(columns[1], metricsMap); 
       break; 
      } 

     } 


    } 
    return countryDataMap; 
} 

我一直在阅读嵌套hashmaps上的各种帖子,但解决方案正在逃避我的把握。任何帮助将不胜感激。我知道这是需要在我的for循环中创建新的Hashmaps,但是当我这样做时,我最终只为每个外键获取一个嵌套的键/值数据点(1行)。

+0

嗯,首先,你不能使用同样的内部地图实例把它在外部映射的不同钥匙的东西。但是,这将成为下一个问题,现在你将内部映射放在与你正在迭代的索引'i'无关的同一个键'columns [1]'处,你正在使用与键相同的错误因此,如果没有运行调试器,我现在至少发现3个错误,首先你需要做的是,在这里发布一个问题就是运行一个调试器! –

+0

想要更多的问题吗?为什么要使用'columns [i]。等于(null)'而不仅仅是'columns [i]!= null'?你认为你让代码更聪明,没有你引入了NPE和无意义的检查:如果'columns [i]'为null,这个检查将会f全部都是NPE,这也使得它无用。调试器! –

+0

好吧,看看你的'break'键中的索引1和2看起来没问题,但这是编写代码的一种非常麻烦的方式。但是问题是:你需要为每一行初始化一个新的内部映射,而不是重复使用同一个对象。 –

回答

0

您只使用一个内部HashMap(metricsMap),并且您始终只将此一个映射放入countryDataMap。您应该为每个外键创建一个新的HashMap作为值。在将新的HashMap放入外部映射之前,您应该检查是否已经有密钥的内部HashMap并重新使用此映射。就像这样:

HashMap<String, Float> metricsMap; 
if (countryDataMap.containsKey(columns[1]) { 
    metricsMap = countryDataMap.get(columns[1]); 
} 
else { 
    metricsMap = new HashMap<>(); 
} 
// use metricsMap 

或更简洁(altough简洁并不总是更好的可读性):

HashMap<String, Float> metricsMap = countryDataMap.containsKey(columns[1]) ? countryDataMap.get(columns[1]) : new HashMap<>(); 
0

谢谢大家。当你回答我的时候,我一直在寻找并解决这个问题。 (我现在就回去看看你指出奥列格。谢谢!

public static HashMap<String, HashMap<String, Float>> loadPoliticalStabilityFromCSV(PApplet p, String filename){ 
    // HashMap key: country ID and data: some political stability measure 
    HashMap<String, HashMap<String, Float>> countryDataMap = new HashMap<>(); 
    //HashMap<String, Float> metricsMap = new HashMap<>(); 

    // pull in the lines from the csv file 
    String[] rows = p.loadStrings(filename); 
    // Reads country name from CSV row 
    for (String row : rows){    

     // split row by commas not in quotations      
     String[] columns = row.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); 
     // Grab Data Point 
     for (int i = columns.length -1; i > 3; i--){ 
      HashMap<String, Float> inner = countryDataMap.get(columns[1]); 
      if (inner == null){ 
       inner = new HashMap<>(); 
       countryDataMap.put(columns[1], inner); 
      } 
      inner.put(columns[2], Float.parseFloat(columns[i])); 
      break;    

     } 


    } 
    return countryDataMap; 
}