2014-07-08 31 views
2

我想在哈希表中添加相同鍵的值。例如:我如何總結與哈希映射中的重複鍵相關聯的值

ABC --> 123 
    DEF --> 456 
    ABC --> 123 
    XXX --> 111 
    XXX --> 222 

應該變成:

ABC --> 246 
    DEF --> 456 
    XXX --> 333 

下面的代碼我到目前爲止:

public class Reading { 


@SuppressWarnings("unchecked") 
public static void main(String[] args) throws IOException { 
    //Create hashmap to store the the string and int in the excel sheet 
    HashMap<String, Integer> hm = new HashMap<String, Integer>(); 

    String key = null; 
    int value = Integer.MIN_VALUE; 
    // String chkeq; // this 

    @SuppressWarnings("rawtypes") 
    ArrayList list = new ArrayList(); 

    // File path or EXCEL file 
    FileInputStream fos = new FileInputStream(
      "/Users/SG/Desktop/tester.xls"); 

    // Sheet is the individual sheet that the data is coming from, in this 
    // case Sheet1 

    try { 

     // Put the XLS file into 
     HSSFWorkbook workbook = new HSSFWorkbook(fos); 

     // Get first sheet from the project 
     HSSFSheet sheet = workbook.getSheetAt(0); 

     // Go through each row 
     Iterator<Row> rowIterator = sheet.iterator(); 
     while (rowIterator.hasNext()) { 


      Row row = rowIterator.next(); 

      // Go through each column in the rows 
      Iterator<Cell> cellIterator = row.cellIterator(); 
      while (cellIterator.hasNext()) { 

       Cell cell = cellIterator.next(); 

       switch (cell.getCellType()) { 
        case Cell.CELL_TYPE_BOOLEAN: 
         //System.out.print(cell.getBooleanCellValue() + "\t\t"); 

         list.add(cell.getBooleanCellValue()); 
         break; 
        case Cell.CELL_TYPE_NUMERIC: 
         //System.out.print(cell.getNumericCellValue() + "\t\t"); 

         value = (int) cell.getNumericCellValue(); 
         list.add(cell.getNumericCellValue()); 
         break; 
        case Cell.CELL_TYPE_STRING: 
         //System.out.print(cell.getStringCellValue() + "\t\t"); 
         key = cell.getStringCellValue(); 
         //chkeq = cell.getStringCellValue(); 

         /* for (int i = 0; cellIterator.hasNext(); i++) { 
          if (chkeq == cellIterator.next().getStringCellValue()) { 
           System.out.println("same" + cell.getStringCellValue()); 

          } 

         }*/ 

         list.add(cell.getStringCellValue()); 
         break; 
       } 

       if (key != null && value != Integer.MIN_VALUE) { 
        hm.put(key, value); 
        key = null; 
        value = Integer.MIN_VALUE; 
       } 

      } 
      //System.out.println(""); 

     } 
     for (String keys : hm.keySet()) 
      System.out.println(keys + ":" + hm.get(keys)); 
     fos.close(); 
     // System.out.println(list); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

注:我使用的Apache POI從Excel表中提取數據。

代碼輸出這樣的:

DEF --> 456 
ABC --> 123 
XXX --> 222 

這一切正在做的是覆蓋掉投入HashMap的平等關鍵的最後一個單元格。

是否有任何方法來總結的價值,而不是寫他們?

回答

3

看跌覆蓋無論是在地圖上。

您需要獲取密鑰(如果存在)的地圖中的當前值並將其添加到要存儲的值。

if(hm.containsKey(key)) 
    hm.put(key, value + hm.get(key)); 
else 
    hm.put(key, value); 
+0

在代碼中,這會去哪裏?我認爲這將工作,但是當我嘗試它時,另一個鍵打印出'{null = 0,ABC = 123,'等 – LethalUniwhale

+0

它代替了你使用的地方放置... – Cheruvian

+0

替換hm.put(鍵,價值); – Cheruvian

2

簡單,如果存在的鍵值,檢索值,並與新的價值總結,並把它回到地圖相同的密鑰

1

如果數據在Excel中,則數據透視表可以進行添加。

+0

@whoeverupvoted謝謝。我想知道在這裏沒有人能夠欣賞到幾百次代碼中的大部分代碼(這是行不通的)。 – pnuts

+1

感謝您的回答,但我真的需要一個java相關的解決方案,因爲現在我使用的是測試儀,但是當我得到這些公司真正的牀單時,我不會被允許編輯它們(我是一個實習生) – LethalUniwhale

+0

好「藉口「 - 但我會考慮複製它們! – pnuts

3

正如您所看到的那樣,hm.put會覆蓋與鍵關聯的以前的值(如果有的話)。在Java 8中,有一種新方法,合併傳入值與現有值。您可以使用lambda來指定如何完成合並。在這種情況下,合併操作是另外的,所以我們會做:

hm.merge(key, value, (oldVal, newVal) -> oldVal + newVal); 

此前的Java 8,你必須使用條件,如Cheruvian's answer。 (+1)

+0

我很困惑x和y會是什麼。 – LethalUniwhale

+0

@LethalUniwhale一個是現有的,另一個是新的價值。我想我應該更新代碼以使其更清楚。 –

+0

我認爲這將工作,如果我有Java 8,但我現在在學校,我無法更新計算機,因爲.exe的限制(和Cheruvian的答案工作很好) – LethalUniwhale

相關問題