如何基于字符串

时间:2018-01-08 12:11:24

标签: java arrays hashmap

我有记录集,其中包含列产品和状态。

例如:

Product    Status
salt       good
sugar      bad
salt       bad
sugar      good
sugar      good

报告后面我需要根据状态进行汇总,比如

summary good 
product_code   sum
sugar          2
salt           1

summary bad 
product_code   sum
sugar          1
salt           1

我正在使用excel进行报告,从我读到的内容我可以确定使用多少盐的好坏

Collections.frequency(collection, key)

来自here

所以我认为我必须将好的和坏的列表分开,然后计算存在多少盐,糖和任何其他产品。这就是我现在要走的多少

rs = impl.showOnlyColumn(query);;
            ResultSetMetaData metaData = rs.getMetaData();

            int colProduct = 0;
            int colStatus = 0;

            String dataProd="";
            String dataStat="";
            Map<String, String[]> mapinData= new HashMap<String, String[]>();
            //making column name on excel
            int columnCount = metaData.getColumnCount();
            for (int column = 1; column <= columnCount; column++) {
                label = new Label(column - 1, 2, metaData.getColumnName(column));
                excelSheet.addCell(label);

                // check if column is product or status 
                String columnName = metaData.getColumnName(column).toLowerCase();
                if (columnName.contains("produ")) {
                    colProduct = column;
                } else if (columnName.contains("status")) {
                    colStatus = column;
                }
            }
            //render the data
            int r = 3;
            while (rs.next()) {
                for (int column = 0; column <= columnCount - 1; column++) {
                    String cellValue = (rs.getString(column + 1) == null ? "" : rs.getString(column + 1));
                    label = new Label(column, r, cellValue);
                    excelSheet.addCell(label);
                    if (column == colProduct) {
                        dataProd = cellValue;
                    }else if (column==colStatus) {
                        dataStat = cellValue;
                    }
                }        
                mapinData.put(dataStat,dataProd);
                r++;
            }

            myFirstWbook.write();

问题是我不知道如何基于状态名称

创建数组
mapinData.put(dataStat,how to make array here?);

然后我将获取数组,用于计算具有该状态的产品总和

如果有什么我说不好的话请告诉我,非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

使用Map<String, List<String>>代替Map<String, String[]>

rs = impl.showOnlyColumn(query);;
ResultSetMetaData metaData = rs.getMetaData();

int colProduct = 0;
int colStatus = 0;

String dataStat="";
Map<String, List<String>> mapinData= new HashMap<>();
//making column name on excel
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
    label = new Label(column - 1, 2, metaData.getColumnName(column));
    excelSheet.addCell(label);

    // check if column is product or status 
    String columnName = metaData.getColumnName(column).toLowerCase();
    if (columnName.contains("produ")) {
        colProduct = column;
    } else if (columnName.contains("status")) {
        colStatus = column;
    }
}
//render the data
int r = 3;
while (rs.next()) {
    List<String> dataProdList = new ArrayList<>();
    for (int column = 0; column <= columnCount - 1; column++) {
        String cellValue = (rs.getString(column + 1) == null ? "" : rs.getString(column + 1));
        label = new Label(column, r, cellValue);
        excelSheet.addCell(label);
        if (column == colProduct) {
            dataProdList.add(cellValue);
        } else if (column == colStatus) {
            dataStat = cellValue;
        }
    }
    dataProdList.addAll(mapinData.getOrDefault(dataSet, new ArrayList<>()));
    mapinData.put(dataStat, dataProdList);
    r++;
}

myFirstWbook.write();