Thursday, March 5, 2009

Source code TreeMap

package myutil;

import java.util.TreeMap;

public class TreeMapEx1 {
public static void main(String[] args)
TreeMap map=new TreeMap();
map.put(new Integer(100),"orange");
map.put(new Integer(200) ,"apple");
map.put(new Integer(300),"grapes");

//enter the existing key it overirde old data
System.out.println(map.put(new Integer(300),"banana"));
System.out.println("The TreeeMap Is "+map);

//try to add hetrogineous(different) objects as value
map.put(new Integer(400),new Integer(10)); // no error

// try to add hetrogineous(different) objects as key
// map.put("fsdf",new Integer(10)); // exception

System.out.println("The TreeeMap Is "+map);
// Try to place the null you will get NullPointerException
// map.put(null,"javasun"); //exception

The TreeeMap Is {100=orange, 200=apple, 300=banana}
The TreeeMap Is {100=orange, 200=apple, 300=banana, 400=10}


Post a Comment