Tuesday, September 13, 2011

TreeMap Example in java


//How to print Name and age of students in sorted order with respect to their name ?
//TreeMapExample.java
import java.util.TreeMap;

public class TreeMapExample
{
public static void main(String arg[])
{
TreeMap treeMap = new TreeMap();

treeMap.put("Bappa", 23);
treeMap.put("Tarun", 25);
treeMap.put("Arun", 27);
treeMap.put("Arup", 22);
treeMap.put("Barun", 29);
treeMap.put("Milan", 29);

for(String name : treeMap.keySet())
{
System.out.println("Name: "+name+" Age= "+treeMap.get(name));
}
}
}

Output =>
Name: Arun Age= 27
Name: Arup Age= 22
Name: Bappa Age= 23
Name: Barun Age= 29
Name: Milan Age= 29
Name: Tarun Age= 25

No comments:

Post a Comment