Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – TreeMap

Declaration

class TreeMap<K, V>

The TreeMap extends abstract class AbstractMap & implements the NavigableSet interface.

Elements insertion order is not maintained.

Elements are sorted in ascending order of keys by default. If we want to alter this sorting order we can specify a comparator when map is created.

List of TreeMap Constructors (CLICK HERE)

List of TreeMap Methods (CLICK HERE)

Non Generic Demo

Non generic TreeMap can contain any type of keys & values.

Example

 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;

 public class TreeMapNonGenDemo 
 {
     public static void main(String args[]) 
     {
         TreeMap tm = new TreeMap();
         tm.put("James",55000);
         tm.put("Rohit",66000);
         tm.put("Poonam",77000);
         tm.put("Daisy", 88000);
         tm.put("Ricky", 99000);
	      
         System.out.println(tm);
	      
         System.out.println("\n After using Iterator :: ");
	       
         Set s = tm.entrySet();
         Iterator itr = s.iterator();
	      
         while(itr.hasNext())
         {
             Map.Entry m = (Map.Entry)itr.next();
             System.out.println("Salary of "+m.getKey() + " is "+m.getValue());
         }
     }
 }

Output

{Daisy=88000, James=55000, Poonam=77000, Ricky=99000, Rohit=66000}

After using Iterator ::
Salary of Daisy is 88000
Salary of James is 55000
Salary of Poonam is 77000
Salary of Ricky is 99000
Salary of Rohit is 66000

Generic Demo

Generic type TreeMap allows storing keys & values only of type, specified during declaration.

Example

 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;

 public class TreeMapGenDemo 
 {
     public static void main(String args[]) 
     {
         TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
         tm.put("James",55000);
         tm.put("Rohit",66000);
         tm.put("Poonam",77000);
         tm.put("Daisy",88000);
         tm.put("Ricky",99000);
	      
         System.out.println(tm);
	      
         System.out.println("\n After using Iterator :: ");
	      
         Set<Map.Entry<String, Integer>> s = tm.entrySet();
         Iterator<Entry<String, Integer>> itr = s.iterator();
	      
         while(itr.hasNext())
         {
             Map.Entry<String, Integer> m = (Entry<String, Integer>)itr.next();
             System.out.println("Salary of "+m.getKey() + " is "+m.getValue());
         }
     }
 }

Output

{Daisy=88000, James=55000, Poonam=77000, Ricky=99000, Rohit=66000}

After using Iterator ::
Salary of Daisy is 88000
Salary of James is 55000
Salary of Poonam is 77000
Salary of Ricky is 99000
Salary of Rohit is 66000

Now we will see, Customized sorting order in TreeMap by implementing comparator.

Example

 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;

 class MyComparator implements Comparator<Integer>
 {
    public int compare(Integer ob1, Integer ob2)
    {
        return ob1.compareTo(ob2);  // ascending order
    //  return ob2.compareTo(ob1);  // descending order
    }
 }

 class TreeMapCustomizedSorting 
 {
    public static void main(String args[]) 
    {
       TreeMap<Integer, String> tm = new TreeMap<Integer,String>(new MyComparator());
       tm.put(5,"Five");
       tm.put(1,"One");
       tm.put(3,"Three");
       tm.put(4,"Four");
       tm.put(2,"Two");
	      
       System.out.println(tm);
	      
       System.out.println("\n After using Iterator :: ");
	      
       Set<Entry<Integer, String>> s = tm.entrySet();
       Iterator<Entry<Integer, String>> itr = s.iterator();
	      
       while(itr.hasNext())
       {
           Entry<Integer, String> m = (Entry<Integer,String>)itr.next();
           System.out.println(m.getKey() + " = "+m.getValue());
       }
    }
 }

Output

{1=One, 2=Two, 3=Three, 4=Four, 5=Five}

After using Iterator ::
1 = One
2 = Two
3 = Three
4 = Four
5 = Five

 

Leave a reply