Visualization of TreeMap and LinkedHashMap in Java
NickName:Ulvon Ask DateTime:2015-06-11T09:06:24

Visualization of TreeMap and LinkedHashMap in Java

I am trying to understand how these data structures actually are visualized. It is said that TreeMap puts the entries in natural order [of keys] and LinkedHashMap puts entries in the order in which they are inserted.

My question is, does the iteration over each of these data structures mean traversing over all the elements spread over all the buckets (or inner array)?

My understanding was that, for instance, in case of TreeMap, elements with an identical hashcode are placed in a Tree structure [of some sort]. Therefore, if a TreeMap has elements in 6 out of 16 indexes [in its bucket array], it would contain 6 Tree's -- one for each.

Similarly, in case of LinkedHashMap (which should have been called DoublyLinkedHashMap in reality), each bucket would have a doubly linked list of its own.

So, how does iteration actually take place? Does it happen over all the elements in all the buckets or only over elements of a single bucket? Am I wrong in my assumption?

P.S. And it seems like in Java 8, HashMap implementation uses a Tree or LinkedList depending on the number of elements for each bucket containing more than 8 or less than 6 elements, respectively!

Copyright Notice:Content Author:「Ulvon」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30769899/visualization-of-treemap-and-linkedhashmap-in-java

Answers
Patricia Shanahan 2015-06-11T15:10:45

The source code for the normal implementations is in the src.zip file that comes with the Oracle JDK installs.\n\nTreeMap: As indicated in its documentation, it is a Red-Black tree. The critical code for simple forwards iteration over the keys is the method static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t). It recursively scans the tree, in the order left-parent-right. It depends for ordering on nodes being inserted in the tree according to their key order.\n\nLinkedHashMap: It has a doubly linked list in arrival order imposed on top of a hash structure with buckets. The nested class Entry extends HashMap.Node to add before and after references. Normal forwards iteration follows the after chain.",


More about “Visualization of TreeMap and LinkedHashMap in Java” related questions

Visualization of TreeMap and LinkedHashMap in Java

I am trying to understand how these data structures actually are visualized. It is said that TreeMap puts the entries in natural order [of keys] and LinkedHashMap puts entries in the order in which...

Show Detail

Difference between HashMap, LinkedHashMap and TreeMap

What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don't see any difference in the output as all the three has keySet and values. What are Hashtables? Map m1 = new HashM...

Show Detail

Convert LinkedHashMap to TreeMap

I have a map LinkedHashMap&lt;String, LinkedHashMap&lt;...&gt;&gt; of maps. What is the best way to make deep conversion of my map to a tree map with respect to values (they should be converted...

Show Detail

Creating a TreeMap visualization

I want the algorithm for creating a Treemap visualization. Something like this: An Easy Way to Make a Treemap Problem is that I do not want to use R ... and I want the source-code. Preferably in ...

Show Detail

Java Map realizations asymptotic complexity (HashMap, LinkedHashMap, TreeMap)

I'm trying to deal with asymptotic complexity of HashMap, LinkedHashMap and TreeMap. On different sites and articles write that on average get = O(1) and at worst - O(n). (if all keys add to one bu...

Show Detail

How to convert Java LinkedHashMap to Scala LinkedHashMap?

I'm new to Scala. I've been trying to convert a java LinkedHashMap to an equivalent collection(LinkedHashMap?) in Scala in order to preserve the insertion order. Tried following things as suggest...

Show Detail

LinkedHashMap and TreeMap which is faster?

I have some data that's going to be coming in sorted order (first the entire set of keys in sorted order and then duplicates in random order). So, I could use both a LinkedHashMap or a TreeMap to

Show Detail

What is increased cost of TreeSet vs LinkedHashSet and TreeMap over LinkedHashMap?

LinkedHashSet - This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashSet, without incurring the increased cost associated with TreeSet. Same is ...

Show Detail

WPF control for treemap visualization with nesting levels

I'm looking for a WPF control for treemap visualization like this: Key point here for me - constant height and variable width (or vice versa). It differs from popular visualization style when we d...

Show Detail

Java TreeMap: Different object after "put"?

I use a class derived from TreeMap with my own comparator as keys in a LinkedHashMap. Working with this construct I found some weird behaviour I could not explain myself. Maybe one of you can help. I

Show Detail