Java collections Framework- TreeMap Class

Java collections Framework- TreeMap Class


Posted in : Java Posted on : December 3, 2010 at 4:58 PM Comments : [ 0 ]

The TreeMap class implements the Map interface by using a tree.

Java collections Framework- TreeMap Class

The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order,
and allows rapid retrieval. You should note that, unlike a hash map, a tree map guarantees that its elements will be sorted in ascending key order.(key, value) pairs are ordered on the key.The implementation is based on red-black tree structure.A Map is an object that maps keys to values.Also called an Associative Array or Dictionary.

The following TreeMap constructors are defined:

TreeMap( ) - This constructor constructs a new, empty tree map, using the natural ordering of its keys.

TreeMap(Comparator comp) - This constructor constructs a new, empty tree map, ordered according to the given comparator.

TreeMap(Map m) - This constructor constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.

TreeMap(SortedMap sm) - This constructor constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. 
TreeMap implements SortedMap and extends AbstractMap. It does not define any additional methods of its own.

The list of methods supported by TreeMap Class are  given below:

put( ) -          Associates the specified value with the specified key in this map. 

clear( ) -       Removes all mappings from the map.

putAll( ) -     Copies all of the mappings from the specified map to the map.

remove( ) -   Removes the mapping for this key from this map if it is present. 

keySet( ) -    Returns a set view of the keys contained in this map. 

entrySet( ) - Returns a collection view of the mappings contained in this map. 

values( ) -    Returns a collection view of the values contained in this map. 

size( ) -        Returns the number of key-value mappings in this map.

Here is the  example for demonstration of the TreeMap interface-

Example:- TreeMapDemo.java

package devmanuals.com;

import java.util.*;

public class TreeMapDemo {
	public static void main(String args[]) {
		TreeMap Tmap = new TreeMap();
		Tmap.put("Gyan", "Singh");
		Tmap.put("Ankit", "Kumar");
		Tmap.put("Arunesh", "Kumar");
		Tmap.put("Anand", "Nagar");
		Tmap.put("Ram", "Prakash");
		Set <Map.Entry<String, String>> set = Tmap.entrySet();
		System.out.println("The Entries of the TreeMap are : ");
		for (Map.Entry<String, String> TM : set) {
			System.out.print(TM.getKey() + " : ");
			System.out.println(TM.getValue());
		}
	}
}
Output:-
The Entries of the TreeMap are : 

Anand : Nagar
Ankit : Kumar
Arunesh : Kumar
Gyan : Singh
Ram : Prakash


Download This Code.

Here is the Example of methods implementation of TreeMap class.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics