Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does deterministic mean?

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?

like image 672
Nathan Schwermann Avatar asked Aug 25 '10 21:08

Nathan Schwermann


2 Answers

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.

like image 141
bukzor Avatar answered Sep 24 '22 06:09

bukzor


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.

like image 45
S.Lott Avatar answered Sep 26 '22 06:09

S.Lott