ave8tor
ave8tor

Reputation: 3

HashMap iterator not starting with first key in collection

I'm running into a very strange occurrence where a HashMap variable

HashMap<Integer, String> locationCatalog = new HashMap<>();

will iterate correctly (ie start at the first numerical key) and sometimes it doesn't. I am iterating through an Excel document (using an external library) to get cell values and, depending on the cell, put it into a HashMap based on the cell's row and column coordinate.

locationCatalog.put(row.getRowNum(), cell.getStringCellValue().trim());

After all the data is populated, I iterate through locationCatalog to get the contents:

Set set = locationCatalog.entrySet();
Iterator trimIterator = set.iterator();
System.out.println(lastLabelName + " - locationCatalog contents (run #1):");
while (trimIterator.hasNext()) {
    Map.Entry locationMap = (Map.Entry)trimIterator.next();
    System.out.println("Key: " + (int) locationMap.getKey() + ", Row: " + ((int) locationMap.getKey() + 1) + ", Location: " + locationMap.getValue().toString());
    ...
}

Here is a print out of a good run:

BALI - locationCatalog contents (run #1):
Key: 16, Row: 17, Location: S082025 E1150531
Key: 17, Row: 18, Location: S082025 E1150531
Key: 18, Row: 19, Location: 
Key: 19, Row: 20, Location: S082025 E1150531
Key: 20, Row: 21, Location:
Key: 21, Row: 22, Location: S082025 E1150531

The anomaly occurs sporadically. It seemed to begin when I would move a cell instead of just copy and paste the contents. Once I figured that out, I adjusted the put method to use just an regular integer that was independent of the Excel source. However, this adjustment did not change the outcome. Here is an example of a bad run:

BALI - locationCatalog contents (run #1):
Key: 16, Row: 17, Location: S08 20 25 E115 05 31 
Key: 17, Row: 18, Location: 
Key: 18, Row: 19, Location: 
Key: 9, Row: 10, Location: S08 20 25 E115 05 31
Key: 10, Row: 11, Location: S08 20 25 E115 05 31
Key: 11, Row: 12, Location: 
Key: 12, Row: 13, Location: S08 20 25 E115 05 31
Key: 13, Row: 14, Location: 
Key: 14, Row: 15, Location: S08 20 25 E115 05 31
Key: 15, Row: 16, Location: S08 20 25 E115 05 31

Without being able to post a screenshot of the Excel file, the cells for this run begin on Row 10 and continue through Row 19. However when it goes through the iteration of locationCatalog, it doesn't start with the lowest key, Key 9 in this case. This is a problem because I have to iterate through this HashMap several times to get a final product and need things to be in an ascending order. After I am finished with the collection, I clear it out to start populating it with a new set of data.

If I cut and paste the same cells into a different row, the outcome (correctly) changes to this:

BALI - locationCatalog contents (run #1):
Key: 4, Row: 5, Location: S08 20 25 E115 05 31
Key: 5, Row: 6, Location: S08 20 25 E115 05 31
Key: 6, Row: 7, Location: 
Key: 7, Row: 8, Location: S08 20 25 E115 05 31
Key: 8, Row: 9, Location: 
Key: 9, Row: 10, Location: S08 20 25 E115 05 31
Key: 10, Row: 11, Location: S08 20 25 E115 05 31
Key: 11, Row: 12, Location: S08 20 25 E115 05 31
Key: 12, Row: 13, Location: 
Key: 13, Row: 14, Location: 

I have been trying for a week now to figure out what is going on, to include changing the parameters of the HashMap, all with no luck. Hopefully someone with more brainpower and Java expertise can lend a helping hand.

Upvotes: 0

Views: 121

Answers (1)

Drunix
Drunix

Reputation: 3343

Your assumption that HashMaps return their entries in some given order is wrong. Use an implementation of OrderedMap like TreeMap instead.

Upvotes: 1

Related Questions