views:

439

answers:

12

I am reading the Java Hashmap documentation but I don't understand this sentence.

Note that the iteration order for HashMap is non-deterministic. If you want deterministic iteration, use LinkedHashMap.

What does deterministic mean?

+9  A: 

In an order which can be "determined" in advance.

Because of the way hashing works, the elements in the map are "scrambled" into arbitrary locations. The scrambling positions cannot easily be determined in advance -- they aren't determinable -- you don't know the resulting order.

S.Lott
+2  A: 

deterministic : can be determined
non-deterministic : can't be determined

BioBuckyBall
+2  A: 

Deterministic means the result is predictable / forseeable.

nonnb
A: 

This is the property of HashMap where elements are not iterated in the same order in which they were inserted as HashMap does not insert elements in order. Therefore the line in documentation

Fazal
+19  A: 

The simplest definition:

Given the same inputs, you always get the same outputs.

Above, it's saying that iterating through the exact same HashMap may give different results at different times, even when you haven't changed anything. Usually that doesn't matter, but if it does, you should use a LinkedHashMap.

bukzor
S. Lott - "idempotent", of a function `f`, means that `f(f(x))=f(x)` for all `x`. It is a different idea to "deterministic".
Hammerite
Let me try again. This sounds like the definition of "no hysteresis" -- the results are consistent with no implied memory.
S.Lott
A: 

In this case it means the order would be predictable, for example in order of when the elements were added to the map, or through comparison of keys (alphabetical order etc.) or something like that.

Since it's non-deterministic, you cannot predict or forsee the order of iteration. It essentially appears random.

Daniel Beck
A: 

It's an algorithm that when given a particular input will produce the same output.

A good example I found:

Consider a shopping list: a list of items to buy.

It can be interpreted in two ways:

* The instruction to buy all of those items, in any order. 
   This is a nondeterministic algorithm.
* The instruction to buy all of those items, in the order given. This is a 
   deterministic algorithm.
Abe Miessler
+7  A: 

In simpler terms: When you call keys(), values() or entrySet() you get back a collection, over which you can iterate. That line is saying you can't expect the order in which the iterator returns objects will be any particular order. Especially, it can be different from both the insertion order and the natural ordering by key values.

If you want the iterator to work in insertion order, use a LinkedHashMap. If you want to iterate by key value, use a TreeMap. Be aware that both of these have slightly worse performance than a plain HashMap, as they both have to do extra work to keep track of the order.

Jim Garrison
A: 

Non deterministic means there is no well defined behaviour.

In the case of the HashMap depending on how you inserted elements you might have the one or other order of iteration.

jdehaan
+2  A: 

Non-deterministic means that there isn't one single result that you can figure it out beforehand. An arithmetical expression, like 1 + 2 or log e, is deterministic. There's exactly one correct answer and you can figure it out upfront. Throw a handful of sand in the air, and where each grain will fall is effectively non-deterministic for any major degree of accuracy.

This probably isn't precisely correct, as you could look at the source code of the underlying library and JVM implementation and there would probably be some way that you could determine the ordering that would result. It might be more correct for them to say,"No particular order is guaranteed," or something of that sort.

What's relevant in this case is that you can't rely on the ordering.

Jesse Millikan
+4  A: 

Strictly speaking, HashMap iteration order is almost certainly not non-deterministic. Like the vast majority of computational processes, if you go through it exactly the same way, the results will be exactly the same. A truly non-deterministic system would incorporate some external random element, which is highly unlikely to be the case here. At least in most cases.

What they really mean, I think, is that just because the map contains a particular set of elements, you shouldn't expect that when you iterate over them they will come out in a particular order. That doesn't mean the order of iteration is random, it just means that as a developer you shouldn't bank on knowing what it is.

In most cases, the reason for this is that there will be some dependency on some implementation details that can vary from platform to platform, and/or on order of access. And the latter may in turn be determined by thread scheduling and event timing, which are innately unpredictable.

In most cases, on any individual platform and with the most common threading model -- a single threaded application -- if you always insert and delete a particular set of things in sequence X you will always get them out in sequence Y. It's just that Y will be so exactly dependent on X, and on the platform, that there's no point even thinking about what it's going to be.

Basically, even though it isn't random, it might just as well be.

walkytalky
+1 for distinguishing between "true random" and "deterministic but unpredictable in advance for a given environment"
raja kolluru
A: 

HashMap doesn't maintain order what you add, if you want your output be the order what you add, you should use LinkedHashMap, so deterministic means output orderdly what you add in.

Here is example: 1.non-deterministic

    HashMap<String, Integer> map = new HashMap<String,Integer>();
    map.put("a",5);
    map.put("b",16);
    map.put("c",46);
    System.out.println(map); //ouptput:{a=5, c=46, b=16}

2.deterministic

HashMap<String, Integer> map = new LinkedHashMap<String,Integer>();
            map.put("a",5);
            map.put("b",16);
            map.put("c",46);
            System.out.println(map); //output:{a=5, b=16, c=46}
jason
Deterministic does not mean you get them out in the order you added them. Rather, it mean for the same set of values, you'll always get them out in the same order.
RHSeeger