在链接的散列图中存储重复的密钥

时间:2018-01-24 12:32:57

标签: duplicates key multimap

Ima读取excel文件作为iam的一部分,创建一个整数和map的链接hashmap。 我的关键是Integer,值是map。

这里的问题是当我的整数值重复时,地图只采用单一条目,但我需要两个条目才能维持。

有人可以帮我解决我的问题。

我已经google了解abour multimap但是因为无法获得要包含的语法和jar文件。附上我的代码以供参考。

package dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MultiMap;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.map.MultiKeyMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;




public class ReadWriteExcelFile {

    static ArrayList<Map<Integer,Map<String,String>>> data_list = new ArrayList<Map<Integer,Map<String,String>>>();

    //static ArrayList<String> names_list = new ArrayList<String>();
    static Map<String,String> names_Map;
    static ArrayList<String> header_list = new ArrayList<String>();

    @SuppressWarnings("deprecation")

    public static void main(String[] k) throws java.lang.Exception {

        ReadWriteExcelFile.readXLSXFile();
        ReadWriteExcelFile.writeXLSXFile();

    }

    public static void readXLSXFile() throws IOException {

        String excelFilePath = "D:/xxxyy.xlsx";

        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

        XSSFSheet firstSheet = workbook.getSheetAt(0);

        Iterator<Row> iterator = firstSheet.iterator();

        int i = 0, j = 0;

        String mainlabel = null;

        while (iterator.hasNext()) {

            Row nextRow = iterator.next();

            Iterator cellIterator = nextRow.cellIterator();



            Map<Integer,Map<String,String>> temp = new LinkedHashMap<Integer,Map<String,String>>();


            while (cellIterator.hasNext()) {
                String mainlabelholder = mainlabel;
                XSSFCell cell = (XSSFCell) cellIterator.next();

                if (cell.getCellType() == cell.CELL_TYPE_STRING ) {
                    mainlabel= cell.getStringCellValue();
                    names_Map = new HashMap<String,String>();
                    names_Map.put(mainlabelholder, cell.getStringCellValue());


                }

                else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC && !cell.equals(null)) {

                    //problem here temp not storing duplicate key 

                    temp.put((int) cell.getNumericCellValue(),names_Map);




                }

            } // end of cell iterator


            if (temp.size() != 0)
                data_list.add(temp);
            mainlabel=null;
            System.out.println("data List" + data_list);
            System.out.println("data List" + data_list.size());
            System.out.println("");

        } // end of iterator

    }

    static String year = "2018";

    static String fiscal_year = "2018";

    public static void writeXLSXFile() throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Quota");
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("NAME");
        header.createCell(1).setCellValue("AMOUNT)");
        header.createCell(2).setCellValue("CLOSEDDATE");
        header.createCell(3).setCellValue("TYPE");
        header.createCell(4).setCellValue("OWNERID");
        header.createCell(5).setCellValue("FISCALQUARTER");
        header.createCell(6).setCellValue("FISCALYEAR");
        header.createCell(7).setCellValue("FISCAL");
        header.createCell(8).setCellValue("PAGAMENTO_ID__C");
        header.createCell(9).setCellValue("QUOTA__C");
        int row = 1;
        int day = 1;

            for (int i = 0; i < data_list.size(); i++) {

                for (int j = 0; j < data_list.get(i).size(); j++) {

                    Row temp = sheet.createRow(row++);
                    // temp.createCell(0).setCellValue("Quota " + ((j / 3) + 1)
                    // + " " + fiscal_year);
                    temp.createCell(0).setCellValue("Quota " + ((j + 1) + " " + fiscal_year));
                    temp.createCell(1).setCellValue(data_list.get(i).keySet().toArray()[j].toString());
                    temp.createCell(2).setCellValue((day) + "/" + (j + 1) + "/" + year);
                    // temp.createCell(3).setCellValue(revenue);
                    Map map = (Map) data_list.get(i).values().toArray()[j];
                    //Map<String,String> temp1 = new HashMap<String,String>();
                    //temp1.putAll(data_list.get(i).values().toArray()[j]);
                    //temp1= data_list.get(i).values().toArray()[j];
                    temp.createCell(3).setCellValue(map.values().toArray()[0].toString());
                    // temp.createCell(3).setCellValue(names_list.get(i+1));
                    temp.createCell(4).setCellValue(map.keySet().toArray()[0].toString());
                    temp.createCell(5).setCellValue(((j / 3) + 1));
                    temp.createCell(6).setCellValue(fiscal_year);
                    temp.createCell(7).setCellValue(fiscal_year + " " + ((j / 3) + 1));
                    // temp.createCell(8).setCellValue(names_list.get(i) + " " +
                    // revenue + " " + ((j / 3) + 1) + " " + fiscal_year);
                    //temp.createCell(9).setCellValue(data_list.get(i).get(j));

                     temp.createCell(8).setCellValue(map.keySet().toArray()[0].toString() + " " +
                     map.values().toArray()[0].toString() + " " + ((j / 3) + 1) + " " + fiscal_year);
                    //temp.createCell(9).setCellValue(data_list.get(i).get(j));



                    temp.createCell(9).setCellValue(data_list.get(i).keySet().toArray()[j].toString());
                }
            }
        //}

        try {
            FileOutputStream out = new FileOutputStream(new File("D:\\out\\Quotacsvmodi.xls"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}`

1 个答案:

答案 0 :(得分:0)

看一下What is difference between HashMap and HashMultimap - 一个HashMap每个键只能包含一个值,所以你可能需要Guava提供的MultiMap