Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Jdk6 Tree Map Example

Related Tutorials


 
 

Share on Google+Share on Google+

Tree Map Example

Advertisement
In the following example, we have used the TreeMap method, which stores its elements in a tree and orders its elements based on their values.

Tree Map Example

     

In the following example, we have used the TreeMap method, which stores its elements in a tree and orders its elements based on their values. Here in the example we have used the key of the element to show the values of the element. To retrieve the keys and values use keySet() and values() method respectively. 

This program shows the data elements left after removing the particular element by specifying its key. Using the Iterator interface methods, we can traverse a collection from start to finish and safely remove elements from the underlying Collection.

 Here is the code of program:

import java.util.*;

public class TreeMapExample{
public static void main(String[] args) {
System.out.println("Tree Map Example!\n");
TreeMap <Integer, String>tMap = new TreeMap<Integer, String>();
//Addding data to a tree map
tMap.put(1, "Sunday");
tMap.put(2, "Monday");
tMap.put(3, "Tuesday");
tMap.put(4, "Wednesday");
tMap.put(5, "Thursday");
tMap.put(6, "Friday");
tMap.put(7, "Saturday");
//Rerieving all keys
System.out.println("Keys of tree map: " + tMap.keySet());
//Rerieving all values
System.out.println("Values of tree map: " + tMap.values());
//Rerieving the value from key with key number 5
System.out.println("Key: 5 value: " + tMap.get(5)+ "\n");
//Rerieving the First key and its value
System.out.println("First key: " + tMap.firstKey() + " Value: " 
+ tMap.get(tMap.firstKey()) + "\n");
//Rerieving the Last key and value
System.out.println("Last key: " + tMap.lastKey() + " Value: " 
+ tMap.get(tMap.lastKey()) + "\n");
//Removing the first key and value
System.out.println("Removing first data: " 
+ tMap.remove(tMap.firstKey()));
System.out.println("Now the tree map Keys: " + tMap.keySet());
System.out.println("Now the tree map contain: " 
+ tMap.values() + "\n");
//Removing the last key and value
System.out.println("Removing last data: " 
+ tMap.remove(tMap.lastKey()));
System.out.println("Now the tree map Keys: " + tMap.keySet());
System.out.println("Now the tree map contain: " + tMap.values());
}
}

Download this example.

Output of this program:

C:\vinod\collection>javac TreeMapExample.java

C:\vinod\collection>java TreeMapExample
Tree Map Example!

Keys of tree map: [1, 2, 3, 4, 5, 6, 7]
Values of tree map: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
Key: 5 value: Thursday

First key: 1 Value: Sunday

Last key: 7 Value: Saturday

Removing first data: Sunday
Now the tree map Keys: [2, 3, 4, 5, 6, 7]
Now the tree map contain: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

Removing last data: Saturday
Now the tree map Keys: [2, 3, 4, 5, 6]
Now the tree map contain: [Monday, Tuesday, Wednesday, Thursday, Friday]

C:\vinod\collection>
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 22, 2007

Related Tutorials

Discuss: Tree Map Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
rohit
April 8, 2012
cse

its too lenthy to understand.
ruchi
April 16, 2012
when can we use tree maps

Under what situations can we use a tree map because hash maps also store the data in the form of key value pairs.
Sachin
June 27, 2012
above example

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type TreeMap is not generic; it cannot be parameterized with arguments <Integer, String> The type TreeMap is not generic; it cannot be parameterized with arguments <Integer, String> at collections.TreeMap.main(TreeMap.java:7)
satish
September 10, 2012
where is iterator is in this example???

where is use of iterator in example above described in description tht use of iterator.
DMCA.com