2
\$\begingroup\$

I have an Excel file with 2 columns, names and numbers. I want to know for each name what is the probability for each number. I prefer to do it in Java so I would be able to combine it with the rest of my project.

Actually, I already did it, and it's works, but I think it's done in a primitive and inefficient way.

Can you please suggest a better solution?

I thought about java stream but didn't know how.

My code:

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class HospitalizationDistribution {

    public static void main(String[] args) {
        HashMap<String, ArrayList<Integer>> map = readData("procedure and hospitalization time.xlsx");
        HashMap<String, HashMap<Integer, Double>> allDisributions = makeDisribution(map);
        
        
        HashMap<Integer, Double> example = allDisributions.get("Partial mastectomy");
        System.out.println("Partial mastectomy:");
        for (Entry<Integer, Double> entry : example.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    


    private static HashMap<String, ArrayList<Integer>> readData(String src){
        HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
        
        try {
            File file = new File(src); // creating a new file instance
            FileInputStream fis = new FileInputStream(file); // obtaining bytes from the file
            //creating Workbook instance that refers to .xlsx file  
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet sheet = wb.getSheetAt(0); // creating a Sheet object to retrieve object
            
            String procedure;
            int duration;
            for (Row row : sheet) { // iterating over excel file
                procedure = row.getCell(0).getStringCellValue();
                duration = (int)row.getCell(1).getNumericCellValue();
                
                if(map.get(procedure) == null)
                    map.put(procedure, new ArrayList<Integer>());
            
                map.get(procedure).add(duration);
            }
            
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
        return map;
    }
    

    private static HashMap<String, HashMap<Integer, Double>> makeDisribution(HashMap<String, ArrayList<Integer>> map) {
        HashMap<String, HashMap<Integer, Double>> allDisributions = new HashMap<String, HashMap<Integer,Double>>();
        
        String procedure;
        HashMap<Integer, Double> singleDisribution;
        ArrayList<Integer> durations;
        double sp; // single outcome probability
        for (Entry<String, ArrayList<Integer>> entry : map.entrySet()) {
            procedure = entry.getKey();
            durations = entry.getValue();

            singleDisribution = new HashMap<Integer, Double>();
            sp = 1.0/durations.size();
            
            Double p;
            for (Integer i : durations) { // summing identical outcomes probability
                p = singleDisribution.get(i);
                if(p == null)
                    p = 0.0;
                singleDisribution.put(i, p + sp);
            }
            allDisributions.put(procedure, singleDisribution);
        }
        return allDisributions;
    }

The output:

Partial mastectomy:
3: 0.02233676975945018
4: 0.036082474226804134
5: 0.02920962199312716
6: 0.02233676975945018
7: 0.013745704467353955
8: 0.02577319587628867
9: 0.020618556701030934
10: 0.02233676975945018
11: 0.017182130584192445
13: 0.005154639175257732
.
.
.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.