Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which java data structures have a deterministic order of iteration?

In an interview, I was asked the following question:

Your application requires to store objects such that the order of entries returned while iterating through the structure is deterministic. In other words, if you iterate over the same structure twice, the order of elements returned in both iterations will be the same. Which of the following classes would you use?

Assume that structure is not mutated. (Check ANY that apply)

HashMap 
LinkedHashSet   
HashTable   
LinkedHashMap
TreeSet 
TreeMap 

I suggested using a LinkedHashSet. Was this the correct answer? Why or why not?

like image 398
user2035385 Avatar asked Nov 07 '25 16:11

user2035385


2 Answers

A determenistic order just means that it's constantly reproducible - the same input will always provide the same iteration order. In this case, the answer is "all of the above". Although most Set's and Map's ordering can't be trusted, it is still determenistic, and will remain the same until the underlying implementation is changed (e.g., if you change or upgrade JVMs).

A predictable order is something more, though - it means that the collection guarantees the order items are returned when iterating the collection. Both "linked" types you mentioned above do that - the order that items were inserted to the collection is the order they will be returned when iterating over it. The "tree" types also guarantee a deterministic order of iteration - a sorted one.

like image 181
Mureinik Avatar answered Nov 10 '25 14:11

Mureinik


As noted by @Elliot Frisch, they are all "deterministic" and will iterate in the same order if nothing has changed. That said, to paraphrase Animal House, some collections are more deterministic than others. :-)

Hash... collections have a deterministic iteration order which the JVM can "predict", but is very challenging for a human to predict and not worth the effort. In practice, they are not "predictable". As @Mureinik points out, the order is officially "unspecified" and subject to change of you change JVMs. The API docs describe this as "generally chaotic ordering" and all sane programmers would agree.

Linked... collections have "predictable iteration order" in that they iterate in the order elements were inserted, with the important caveat that if you insert the same element twice it retains the original order. i.e.

add("Tom");
add("Fred");
add("Tom");

would iterate "Tom", "Fred", not "Fred", "Tom"

This is clearly "more predictable" than Hash..., but still a bit challenging if elements get inserted multiple times and ordering is crucial. For stuff like properties files, XML, or JSON, Linked... collections are generally a good choice as they maintain the original order for nicer human viewing and comparison.

Tree... collections iterate the "most predictably", using the ordering provided by a Comparator at construction time, or else the "natural ordering" if the elements are Comparable. Assuming you have a predicable comparison method, they are completely predictable. In the Tom/Fred example, it would always iterate as "Fred", "Tom", unless your Comparator is unusual.

like image 23
user949300 Avatar answered Nov 10 '25 13:11

user949300



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!