0

According to this question: How to preserve insertion order in HashMap?

HashMap makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

But then i have a question about an oracle tutorial

public void updateCoffeeSales(HashMap<String, Integer> salesForWeek)
    throws SQLException {
    ...
    try {
        ...
        for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
            updateSales.setInt(1, e.getValue().intValue());
            updateSales.setString(2, e.getKey());
            updateSales.executeUpdate();
            updateTotal.setInt(1, e.getValue().intValue());
            updateTotal.setString(2, e.getKey());
            updateTotal.executeUpdate();
            con.commit();
        }
    } catch (SQLException e ) {
        ...
    } finally {
       ...
    }

it's from here: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

How do they know that values for updateSales & updateTotal will not be mixed?

Community
  • 1
  • 1
andy007
  • 907
  • 1
  • 15
  • 41

1 Answers1

1

The documentation talks about relative order of key-value pairs. If you add items

 a:b
 c:d
 e:f

to a hash map, you could get them in an arbitrary order when you iterate. For example, you could get

 c:d
 a:b
 e:f

However, this re-ordering cannot break up the pairs. In other words, a will remain paired to b - it wouldn't get re-ordered to correspond to d or to f.

When you iterate over the map's entrySet(), you get an unordered list of pairs. However, the pairs themselves remained paired up: if a certain value is set for a given key, it would be returned as a pair of that key regardless of the order of iteration. Since one should not rely on the natural order of items in a database table either, the particular insertion order makes no difference.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • See, they iterating throw HashMap, and the results can be in an orbitrary order, as you said. But what if the fist pair in iterate block will be for updateTotal? Then the first pair of values will be used for updateSales and it will be completely wrong... – andy007 Sep 21 '14 at 09:03
  • 2
    @Andy What do you mean? Each pair in their demo is used both for updating the sales *and* for updating the total. The table is set up in such a way that the `SALES` column contains the value for last week, and the `TOTAL` contains a running total. – Sergey Kalinichenko Sep 21 '14 at 09:04
  • @Andy Within the **body** of the `for` loop, the element `Map.Entry e` does not change. Both `updateSales` and `updateTotal` will be using the same element `e` in each iteration. The documentation simply means that the order of elements you get from `entrySet()` is arbitrary and not guaranteed, but once you get them and you start iterating over them, the order **will not** change in the middle of iteration. – ADTC Sep 21 '14 at 09:29
  • 1
    @Andy Also you must understand that the tutorial's code is so designed that each pair in the `HashMap` is used for **both** `updateSales` and `updateTotal`. I sense you are misunderstanding that first pair is only for `updateTotal`, and the second pair is only for `updateSales`, or something like that. That is **not** their intention, otherwise the code would be very different. – ADTC Sep 21 '14 at 09:37
  • @ADTC I was just confused by the use of two times the same pair of values, no questions, thanks! – andy007 Sep 21 '14 at 09:45
  • @dasblinkenlight I was just confused by the use of two times the same pair of values, no questions, thanks! – andy007 Sep 21 '14 at 09:46
  • @Andy The same pair can be used as many number of times as you like within the body of the `for` loop. :) – ADTC Sep 21 '14 at 10:00