2

i have recordset that in it contain column product and status.

for example:

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

later in the report i need to make summary based on the status, like this

summary good 
product_code   sum
sugar          2
salt           1

summary bad 
product_code   sum
sugar          1
salt           1

i'm making report in excel, and from what i read i can determine how much salt good or bad using

Collections.frequency(collection, key)

from here

so then i think i have to separate list for good and bad and then count how much salt, sugar and any other product that exist. this is how much i go now

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();

the problem is i don't know how to make an array based on the status name

mapinData.put(dataStat,how to make array here?);

then i will take the array, for counting the sum of the product with that status

if there is something that i don't describe well please tell me, really appreciate your help.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
ikiSiTamvaaan
  • 127
  • 1
  • 14

1 Answers1

1

Use Map<String, List<String>> instead of 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();
zhh
  • 2,346
  • 1
  • 11
  • 22
  • after trying your code, dataprodlist is error maybe because it is inside the for so it cant be accessed out of it, and i cant find mapindata.getordefault do i use wrong import? i'm using import java.util.HashMap; – ikiSiTamvaaan Jan 09 '18 at 03:28
  • ```getOrDefault``` is available in java 8 and I made a mistake this function takes two parameters, I've updated my code. – zhh Jan 09 '18 at 03:31
  • btw, is dataSet is new variable? – ikiSiTamvaaan Jan 09 '18 at 03:43
  • Yep, ```dataSet``` is the key which I added just now. – zhh Jan 09 '18 at 03:44
  • is there a code that run like .getOrDefault() in java 7? – ikiSiTamvaaan Jan 09 '18 at 08:09
  • 1
    You don't have to use ```getOrDefault()```, you can use ```contains()``` and then ```get()```. ```if (mapinData.contains(dataStat)) { dataProdList.addAll(mapinData.get(dataSet)); }``` – zhh Jan 09 '18 at 08:14