久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將與哈希圖中重復出現(xiàn)的鍵關聯(lián)的值相加

How can I sum the values associated with a reoccurring key in a hashmap(如何將與哈希圖中重復出現(xiàn)的鍵關聯(lián)的值相加)
本文介紹了如何將與哈希圖中重復出現(xiàn)的鍵關聯(lián)的值相加的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想在哈希圖中添加相同鍵的值.例如:

I want to add the values of the same keys in a hashmap. For example:

   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() + "		");

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

                        value = (int) cell.getNumericCellValue();
                        list.add(cell.getNumericCellValue());
                        break;
                    case Cell.CELL_TYPE_STRING:
                        //System.out.print(cell.getStringCellValue() + "		");
                        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 工作表中提取數(shù)據(jù).

Note: I have used Apache POI to extract the data from an Excel sheet.

代碼輸出如下:

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

所有這一切都是用相等的鍵覆蓋放入哈希圖中的最后一個單元格.

All this is doing is overwriting the last cell that was put into the hashmap with equal key.

有什么方法可以對這些值求和而不是覆蓋它們?

Is there any way to sum the values instead of writing over them?

推薦答案

Put 會覆蓋地圖中的任何內(nèi)容.

Put overwrites whatever is in the map.

您需要獲取映射中鍵的當前值(如果存在)并將其添加到您要存儲的值中.

You need to get the current value in the map for the key (if it exists) and add it to the value you want to store.

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

這篇關于如何將與哈希圖中重復出現(xiàn)的鍵關聯(lián)的值相加的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 亚洲一区二区三区在线免费观看 | 国产精品久久久久久福利一牛影视 | 在线观看国产 | 呦呦在线视频 | www.国产视频 | 久久6| 国产精品一区二区在线播放 | 免费高潮视频95在线观看网站 | 亚洲一区二区在线播放 | 亚洲精品乱码久久久久久按摩 | 国产成人麻豆免费观看 | 久久成人免费 | 中文字幕乱码一区二区三区 | 一区二区三区在线播放 | 亚洲夜射 | 小早川怜子xxxxaⅴ在线 | 国产精品久久久久久吹潮 | 亚洲精品一区中文字幕乱码 | 国产三级在线观看播放 | 欧美专区在线视频 | 黄色免费观看 | 中文字幕在线播放第一页 | 久久综合九色综合欧美狠狠 | 久久精品免费观看 | 一区日韩| 久久久一二三 | 小草久久久久久久久爱六 | 国产激情精品视频 | 国产一区二区三区在线看 | 成人久久久| 极品一区 | 日韩在线观看 | 性一交一乱一伦视频免费观看 | 国产成人免费网站 | 久草视频观看 | 99精品免费久久久久久日本 | 日韩视频在线一区二区 | 国产在线视频在线观看 | 波多野吉衣在线播放 | 成人av免费播放 | 久久精品国产久精国产 |