<div dir="ltr" style="text-align: left;">
<p>Both <i><b>TreeMap </b></i>&; <i><b>HashMap </b></i>are two different implementations of the <i><b>Map </b></i>interface. Even though this post is titled “<i><b>TreeMap </b></i>vs <i><b>HashMap</b></i>” I would like to say how they are connected and how much similar they are.</p>
<p>Both <i><b>TreeMap </b></i>&; <i><b>HashMap </b></i>are not synchronized. To make it synchronized we have to explicitly call Collections.synchronizedMap( mapName). Both supports “fail-fast” iterators. Both of them doesn’t support duplicate keys.</p>
<p><i><b>HashMap</b></i></p>
<p><i><b>1. HashMap </b></i>allows null as both keys and values.<br />
<i><b>2. HashMap</b></i> is useful when we need to access the map without considering how they are added to the map (means, unordered lookup of values using their keys).<br />
<i><b>3. HashMap</b></i> is not synchronized while it is being looked up.<br />
<i><b>4. HashMap</b></i> doesn’t allow duplicated entries.</p>
<div id="ads-id" align="center"></div>
<p>The performance of <i><b>HashMap </b></i>is based on two optional parameters which we can specify during the creation of the <i><b>HashMap</b></i>. Initial capacity &; load factor. Initial capacity is the bucket size assigned to a <i><b>HashMap</b></i> during its creation. Load factor decides when the <i><b>HashMap </b></i>needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.</p>
<p><i><b>TreeMap</b></i></p>
<p>The basic difference between <i><b>HashMap &; TreeMap</b></i> is that,<br />
1. in a <i><b>TreeMap </b></i>the elements are stored in a tree.<br />
2.<i><b>TreeMap </b></i>allows us to retrieve the elements in some sorted order defined by the user. So we can say that <i><b>TreeMap </b></i>is slower than <i><b>HashMap</b></i>. This is the only implementation based on <i><b>a SortedMap </b></i>interface.</p>
<p><i><b>TreeMap </b></i>allows us to specify an optional <i><b>Comparator </b></i>object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.</p>
<pre class="highlight">public interface Comparator 
{ 
 public int compare (Object object1, Object object2); 
 public boolean equals (Object object); 
} 
</pre>
<p>If we are not specifying the comparator, <i><b>TreeMap </b></i>will try to sort the keys in the natural order. Means will consider them as instances of <i><b>Comparable </b></i>interface.</p>
<pre class="highlight">public interface Comparable 
{ 
 public int compareTo (Object objectToCompare); 
} 
</pre>
<p>Classes like <i><b>Integer, String, Double</b></i> etc implement <i><b>the Comparable </b></i>interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the <i><b>Comparable </b></i>interface.</p>
<pre class="highlight">public class MyCustomKey implements Comparable 
{ 
 private int value; 
 public MyCustomKey(int value) 
 { 
 this.value = value; 
 } 
 
 public int compareTo (MyCustomKey key) 
 { 
 int comparison = 0; 
 
 // Note: 
 // Return -1 if this.value <; key.value 
 // Return 0 if this.value = key.value 
 // Return 1 if this.value >; key.value 
 
 return (comparison); 
 } 
} 
</pre>
<p>A common mistake that everyone does is not to override the <i><b>hashcode()</b></i>. If we are failing to do so, <i><b>map.get(new MyCustomKey(<;value>;))</b></i>; may not give you what you were expecting. So it is always advisable to override the <i><b>hashCode()</b></i> if objects of that class are being used as a key.</p>
<pre class="highlight">public class MyCustomKey implements Comparable 
{ 
 private int value; 
 public MyCustomKey(int value) 
 {} 
 public int compareTo (MyCustomKey key) 
 {} 
 
 public int hashCode() 
 { 
 // Note: 
 // If two objects are equal then their corresponding hashCode () 
 // should return the same value too. 
 return (this.value * 199); 
 } 
} 
</pre>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/treemap-class-in-collection-framework/">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/treemap-class-in-collection-framework/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '410'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…