<div dir="ltr" style="text-align: justify;" trbidi="on"><b id="h1"><br />
<i>TreeMap </i>class</b></p>
<ul>
<li>A <b>TreeMap </b>contains values based on the key. It implements the <b>NavigableMap </b>interface and extends <b>AbstractMap </b>class.</li>
<li>It contains only unique elements.</li>
<li>It cannot have null key but can have multiple null values.</li>
<li>It is same as <b>HashMap </b>instead maintains ascending order. </li>
</ul>
<p>The <b>TreeMap </b>class implements the Map interface by using a tree. A <b>TreeMap </b>provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/06/treemap.jpg"/></div>
<p>
 You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order.</p>
<p> The <b>TreeMap </b>class supports four constructors. The first form constructs an empty tree map that will be sorted by using the natural order of its keys:</p>
<pre class="highlight" name="code">TreeMap( ) 
</pre>
<div align='center' id="ads-id"></div>
<p>The second form constructs an empty tree-based map that will be sorted by using the <b>Comparator </b>comp:</p>
<pre class="highlight" name="code">TreeMap(Comparator comp) 
</pre>
<p>The third form initializes a tree map with the entries from m, which will be sorted by using the natural order of the keys:</p>
<pre class="highlight" name="code">TreeMap(Map m) 
</pre>
<p>The fourth form initializes a tree map with the entries from sm, which will be sorted in the same order as sm:</p>
<pre class="highlight" name="code">TreeMap(SortedMap sm) 
</pre>
<p><b>Apart from the methods inherited from its parent classes, <i>TreeMap </i>defines following methods:</b></p>
<table class="src" style="-webkit-text-stroke-width: 0px; background-color: #f7f7f7; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); color: black; font-family: Helvetica, Arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin: 8px 0px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; vertical-align: top; white-space: normal; widows: auto; width: 560px; word-spacing: 0px;">
<tbody style="margin: 0px; padding: 0px;">
<tr style="margin: 0px; padding: 0px;">
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px; text-align: left;">SN</th>
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px; text-align: left;">Methods with Description</th>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">1</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void clear()</b><br />
Removes all mappings from this TreeMap.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">2</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object clone()</b><br />
Returns a shallow copy of this TreeMap instance.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">3</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Comparator comparator()</b><br />
Returns the comparator used to order this map, or null if this map uses its keys&#8217; natural order.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">4</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean containsKey(Object key)</b><br />
Returns true if this map contains a mapping for the specified key.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">5</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean containsValue(Object value) ;</b><br />
Returns true if this map maps one or more keys to the specified value.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">6</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Set entrySet() ;</b><br />
Returns a set view of the mappings contained in this map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">7</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object firstKey() ;</b><br />
Returns the first (lowest) key currently in this sorted map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">8</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object get(Object key) ;</b><br />
Returns the value to which this map maps the specified key.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">9</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">SortedMap headMap(Object toKey) ;</b><br />
Returns a view of the portion of this map whose keys are strictly less than toKey.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">10</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Set keySet()</b><br />
Returns a Set view of the keys contained in this map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">11</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object lastKey() ;</b><br />
Returns the last (highest) key currently in this sorted map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">12</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object put(Object key, Object value) ;</b><br />
Associates the specified value with the specified key in this map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">13</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void putAll(Map map) ;</b><br />
Copies all of the mappings from the specified map to this map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">14</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object remove(Object key) ;</b><br />
Removes the mapping for this key from this TreeMap if present.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">15</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">int size() ;</b><br />
Returns the number of key-value mappings in this map.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">16</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">SortedMap subMap(Object fromKey, Object toKey) ;</b><br />
Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">17</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">SortedMap tailMap(Object fromKey) ;</b><br />
Returns a view of the portion of this map whose keys are greater than or equal to fromKey.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;">18</td>
<td style="border-collapse: collapse; border: 1px solid rgb(214, 214, 214); margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Collection values() ;</b><br />
Returns a collection view of the values contained in this map.</td>
</tr>
</tbody>
</table>
<p>
<b>Example:</b></p>
<pre class="highlight" name="code">import java.util.*; 
class TreeMapDemo 
{ 
 public static void main(String args[]) 
 { 
 TreeMap<; String,Integer>; tm = new TreeMap<; String,Integer>;(); 
 tm.put("a",new Integer(100)); 
 tm.put("b",new Integer(200)); 
 tm.put("c",new Integer(300)); 
 tm.put("d",new Integer(400)); 
 
 Set<; Map.Entry<; String,Integer>; >; st = tm.entrySet(); 
 for(Map.Entry me:st) 
 { 
 System.out.print(me.getKey()+":"); 
 System.out.println(me.getValue()); 
 } 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
a 100<br />
b 200<br />
c 300<br />
d 400</div>
<p></p>
<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/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs 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/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>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">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: '411'});
});
</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…