Codelytics

Visualise your geek speak

  • Blog
  • Terms
  • Privacy
  • Contact

java.util.TreeMap visualized: interactive view from a new perspective

September 6, 2018 by Adrian Bartlett Leave a Comment

java.util.TreeMap stores Key Value pairs like java.util.HashMap, and enables the retrieval of a Value given a Key.  TreeMap differs from HashMap in that the Keys are ordered by their natural order.  Internally the TreeMap maintains a Tree data structure, used to order and locate keys.

This tutorial, complete with an easy to understand and interactive UML visualization, explains how TreeMap works.

This is the another instalment of our java.util.Map series.  Earlier parts include:

  • Hashmap Introduction Tutorial
  • Hashmap and the Importance of HashCode
  • Java 8, Map::computeIfAbsent Tutorial

Class Diagram


Take a look at the Class Diagram above.  You may note the following interesting points in this diagram:

  • java.util.TreeMap implements java.util.SortedMap: All sorted maps, order keys by their natural ordering. E.g. natural ordering of strings would result in alphabetic ordering
  • PersonKey implements java.lang.Comparable: this enables PersonKey instances to be sorted.  More specifically PersonKey implements the java.lang.Comparable::compareTo method.  The method returns 0, if the PersonKey is equal to the object passed in.  Alternatively compareTo should return a positive integer if PersonKey is greater than the object passed in, or a negative integer should be returned.

Step through TreeMap in action


Step through the visualisation from the above image, and read the yellow comments and the associated code.

When the code is executed, the following is output.  Note how the results have been ordered.  Observe the PersonKey::compareTo method and note how the last name is primarily used to order, and, secondarily, if the last names are equal, the first name is then used to order.

Last Name  First Name  Rating
---------------------------------
Jackson    Bill        2
Smith      Andrew      1
Smith      Bill        5
Smith      Jack        4

Do you like this Codelytics’ visualization?

If you like our visualizations, be sure to subscribe to our  mailing list below.  With your free subscription, we will periodically send you alerts about new tutorials and visualizations posted here.

Share this:

  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Filed Under: Collections, Java Tagged With: Collections, Hashmap, Hashmap Tutorial, java.util.Map series, TreeMap

Connect with us

  • Facebook
  • Google+
  • Twitter
  • YouTube

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Facebook
  • Google+
  • Twitter
  • YouTube

Subscribe to the blog

Recent posts

  • LinkedHashMap Tutorial
  • java.util.TreeMap visualized: interactive view from a new perspective
  • Constructor & Setter Dependency Injection
  • 4 Reasons to use a Java Semaphore
  • Java 8: How To Retrieve Absent Values From HashMap

Search

  • Blog
  • Terms
  • Privacy
  • Contact

All website content, including software, (excluding that owned by Wordpress and Centric Theme on Genesis Framework) is owned by Codelytics Pty Ltd and/or its affiliates, all rights reserved and protected by Australian copyright laws and relevant international treaty provisions. Any reproduction, redistribution and/or reverse engineering is expressly prohibited. For further details see the terms and conditions page on this site.
© Codelytics Pty Ltd, Adrian Bartlett, 2013

Copyright © 2022 · Centric Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.