Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

java - 读取多个值并将其存储在HashMap Apache POI中(Read and store the multiple value in HashMap Apache POI)

In Excel I have data like below, where first row is header.

(在Excel中,我有如下数据,其中第一行是标题。)

           ProductName                  ProductSubType 
    COMPREHENSIVE INSURANCE         COMPREHENSIVE INSURANCE
    COMPREHENSIVE INSURANCE         DRIVER & PASSENGER
    COMPREHENSIVE INSURANCE         THIRD PARTY INSURANCE
    DEBT PROTECTION                 CREDIT LIFE
    DEBT PROTECTION                 RETRENCHMENT

Below is the code that I have tried.

(下面是我尝试过的代码。)

 XSSFSheet myExcelSheet3 = xssfWorkbook.getSheet("Sheet3");
  XSSFRow xssfRow3 = myExcelSheet3.getRow(0);

    Integer columnIndexKey = null;
    for (Cell cell : xssfRow3) {
        if (cell.getStringCellValue().equalsIgnoreCase("ProductType")) {
            columnIndexKey = cell.getColumnIndex();
            break;
        }
    }
    Set<String> stringKey = new HashSet<>();
    if (columnIndexKey != null) {
        for (Row row : myExcelSheet3) {
            if (row.getRowNum() == 0) {
                continue;
            }
            Cell cell = row.getCell(columnIndexKey);
            stringKey.add(getCellData(xssfWorkbook, cell));
        }
    }
    List<String> stringKeyList = new ArrayList<>(stringKey);
    Collections.sort(stringKeyList);

Get the values ie ProductSubType as List

(获取值,即ProductSubType作为列表)

List<String> stringValue = new ArrayList<>();
        if (columnIndexValue != null) {
            for (Row row : myExcelSheet3) {
                if (row.getRowNum() == 0) {
                    continue;
                }
                Cell cell = row.getCell(columnIndexValue);
                stringValue.add(getCellData(xssfWorkbook, cell));
            }
        }

and finally added the key and values in HashMap

(最后在HashMap中添加了键和值)

 Map<List<String>, List<String>> stringListMap = new HashMap<>();
  stringListMap.put(stringKeyList,stringListValue);
  stringListMap.forEach((k,v) -> System.out.println("Key = "+ k + ", Value = " + v));

The output I am getting is

(我得到的输出是)

Key = [COMPREHENSIVE INSURANCE, DEBT PROTECTION], Value = [COMPREHENSIVE INSURANCE, DRIVER & PASSENGER, THIRD PARTY INSURANCE, CREDIT LIFE, RETRENCHMENT]

However I want output in below format.

(但是我想以以下格式输出。)

COMPREHENSIVE INSURANCE -> COMPREHENSIVE INSURANCE, DRIVER & PASSENGER, THIRD PARTY INSURANCE
DEBT PROTECTION -> CREDIT LIFE, RETRENCHMENT.
  ask by Deepak translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a Map<String, List<String>> instead of Map<List<String>, List<String>> and reorder your code like this.

(使用Map<String, List<String>>代替Map<List<String>, List<String>>并像这样重新排序代码。)

//Calculate columnIndexKey and columnIndexValue

Map<String, List<String>> result = new HashMap<>();
if (columnIndexKey != null && comumnIndexValue != null) {
    for (Row row : myExcelSheet3) {
        if (row.getRowNum() == 0) {
            continue;
        }
        Cell cell = row.getCell(columnIndexKey);
        Cell valueCell = row.getCell(columnIndexValue);

        String key = getCellData(xssfWorkbook, cell);
        String value = getCellData(xssfWorkbook, valueCell);

        if (!result.contains(key)) {
            result.put(key, new ArrayList<>());
        }
        result.get(key).add(value);
    }
}

result.foreach((k, v) -> System.out.println("Key = "+ k + ", Value = " + v));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...