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.

1
  • 3
    Use Map<String, List<String>> instead. Commented Jan 8, 2018 at 12:24

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

6 Comments

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;
getOrDefault is available in java 8 and I made a mistake this function takes two parameters, I've updated my code.
btw, is dataSet is new variable?
Yep, dataSet is the key which I added just now.
You don't have to use getOrDefault(), you can use contains() and then get(). if (mapinData.contains(dataStat)) { dataProdList.addAll(mapinData.get(dataSet)); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.