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