A Simple TreeMap Java Program

A Simple TreeMap Java Program

Previous Home Next

 

This Example shows how to use the TreeMap in java .

This example will shows how to add the values to the TreeeMap in java and how to retrieve.
put() and get() are used in this example
The put method returns object if the which is a value previously tied to the key. it will return null if value is not map to the TreeMap
 This example will shows how to add the values to the TreeeMap in java and how to retrieve.
Put () is used to put the values and get () is used to get the values from the  TreeMap in java
The value must be casted to the original class.

Retrieve value using Object get(Object key) method of Java TreeMap class.
Return type of get method is an Object

 

package Example;
import java . util.TreeMap;
public class TreeMapExample {

/**
* @param args
*/
public static void main(String[] args) {
TreeMap tmp= new TreeMap();
tmp.put("Six", new Integer(6) );
tmp.put("Seven", new Integer(7) );
tmp.put("eight", new Integer(8) );
Object obj =tmp.get("eight");
System.out.println("The value is :" + obj);
// TODO Auto-generated method stub

}

}

The value is :8

Previous Home Next