【问题标题】:Storing duplicate keys in a linked hashmap在链接的哈希图中存储重复的键
【发布时间】:2018-01-24 12:32:56
【问题描述】:

Ima 读取一个 excel 文件作为 iam 创建整数和映射的链接哈希图的一部分。 我的键是整数,值是映射。

这里的问题是当我的整数值被复制时,地图只需要一个条目,但我需要两个条目来维持。

有人可以帮忙解决我的问题吗?

我已经用谷歌搜索了关于 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();
        }
    }
}`

【问题讨论】:

  • 您能否用简单的话澄清您的问题并将代码示例简化为minimal, complete, and verifable example
  • Map> temp = new LinkedHashMap>();是我的地图和 temp.put((int) cell.getNumericCellValue(),names_Map);我试图将整数和字符串映射,字符串作为键值对。但是当我的键被复制时,映射只包含一个条目,但我想要两者

标签: duplicates key multimap


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2021-11-03
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多