Core Java Tutorial

Core Java Tutorial

Java TreeMap Example Program

Java TreeMap:

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

package com.candidjava.core;
import java.util.TreeMap;
 
 
public class TreeMapExample {
 
  
  public static void main(String[] args) {
    
    TreeMap<String,String> tm=new TreeMap<String,String>();
    tm.put("name","candid");
    tm.put("email","candid@gmail.com");
    tm.put("password", "123456");
    tm.put("street", "shanthi nagar");
    tm.put("password", "789");
    tm.put("altemail", null);
    tm.put("altph", null);
    
    
    System.out.println("TreeMap ..  "+tm);
    System.out.println("size ... "+tm.size());		
    
  }
  
}
Output:
TreeMap ..  {altemail=null, altph=null, email=candid@gmail.com, name=candid, password=789, street=shanthi nagar}
size ... 6