0

I am building a program that prints out info into an .xlsx file using Apache POI and it seems to be working great, but a hiccup has developed as I have added more lines to the print out process. I am using a HashMap with an integer key and a string object array to store the info and then it writes to the excel file. Everything works great until the line with key 17. The program starts to mix up the lines and prints them out of order. Here is the code and below that is how it prints out. Notice how lines 17 and 18 get mixed and how the 2 blank line are left out. What am I missing that could be causing this craziness? Thanks all!

            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Estimate");

            //Create a new row in current sheet
            Row row = sheet.createRow(0);

            //Create a new cell in current row
            Cell cell = row.createCell(0);

            //Set value to new value
            cell.setCellValue("");



            Map<Integer, Object[]> 
            data = new HashMap<Integer, Object[]>();
            data.put(0, new Object[] {"", "SIZE 1 (in inches)", "SIZE 2 (in inches)", "PRICE"});
            data.put(1, new Object[] {"P-BOARD:" });
            data.put(2, new Object[] {""});
            data.put(3, new Object[] {"","AMOUNT PER SQFT", "COUNT", "PRICE"});
            data.put(4, new Object[] {"SCREWS:"});
            data.put(5, new Object[] {""});
            data.put(6, new Object[] {"", "PRICE 50 lb BAG", "PRICE 25 lb BAG"});
            data.put(7, new Object[] {"MORTAR:"});
            data.put(8, new Object[] {""});
            data.put(9, new Object[] {"", "PRICE"});
            data.put(10, new Object[] {"TROWEL"});
            data.put(11, new Object[] {""});
            data.put(12, new Object[] {"", "PRICE"});
            data.put(13, new Object[] {"GROUT FLOAT"});
            data.put(14, new Object[] {""});
            data.put(15, new Object[] {"", "PRICE"});
            data.put(16, new Object[] {"LARGE BUCKET"});
            data.put(17, new Object[] {""});
            data.put(18, new Object[] {""});
            data.put(19, new Object[] {"TILE DIMENSION 1", "TILE DIMENSION 2", "BRAND", "PRICE"});

            Set<Integer> keyset = data.keySet();

            int rownum = 0;

            for (Integer key : keyset) 
            {

                Row row1 = sheet.createRow(rownum++);

                Object [] objArr = data.get(key);

                int cellnum = 0;

                for (Object obj : objArr) 
                {
                    Cell cell1 = row1.createCell(cellnum++);

                    if(obj instanceof Date) 
                        cell1.setCellValue((Date)obj);
                    else 
                        if(obj instanceof Boolean)
                        cell1.setCellValue((Boolean)obj);
                    else 
                        if(obj instanceof String)
                        cell1.setCellValue((String)obj);
                    else 
                        if(obj instanceof Double)
                        cell1.setCellValue((Double)obj);
                }
            }

enter image description here

Sev
  • 883
  • 1
  • 14
  • 34

1 Answers1

4

A java HashMap has no inherent order. See here for a longer answer. Why are you using a Map in the first place and not just a List?

If you want to continue using a HashMap. Order your keySet before iterating it.

Here is how you can get a sorted list of your Integer keys:

List<Integer> keys = new ArrayList<>( data.keySet() );
Collections.sort( keys );
Community
  • 1
  • 1
hagbard
  • 695
  • 3
  • 13
  • I am just now learning how to use both the hashmap and POI so this is an example method I saw used so that is why I am using it. So by the link you posted I should use a LinkedHashMap? Or would List work better? – Sev Sep 12 '13 at 21:44
  • Fair enough :) I've added an example how you can get a sorted list of your map keys. – hagbard Sep 12 '13 at 21:46
  • Ah. I shall implement your suggestion and do more reading up on hashmaps. Thanks very much. – Sev Sep 12 '13 at 21:48
  • 3
    @Nerves82 Or use a `TreeMap` that already sorts based on key. – Luiggi Mendoza Sep 12 '13 at 21:52
  • Changing the HashMap to a LinkedHashMap seemed to solve the issue altho I will read up on the treemap too and see which will be better. And your further suggestions are always welcome. :) Thanks again. – Sev Sep 12 '13 at 21:55
  • You might have a look at the highest rated answer for the question I linked above, it also lists the differences between some Map implementations. So you know what you get yourself into. – hagbard Sep 12 '13 at 21:56