Thursday 5 June 2014

TreeMap (Map m)

public TreeMap(Map<? extends K, ? extends V> m)
Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.

import java.util.*;

class Employee implements Comparable<Employee>{
 int id;
 String name;
 
 Employee(int id, String name){
  this.id = id;
  this.name = name;
 }
 
 public int compareTo(Employee o){
  return this.name.compareTo(o.name);
 }
 
}

import java.util.*;

class TreeMapConstructor3{
 public static void main(String args[]){
  TreeMap<Employee, Integer> myMap;
  Map<Employee, Integer> map;
  Set<Employee> keys;
  Iterator<Employee> iter; 
  Employee tmp;  
  
  map = new HashMap<> ();
  
  /* Insert data to myMap */
  map.put(new Employee(4, "Krishna"), 15000);
  map.put(new Employee(2, "Anand"), 95000);
  map.put(new Employee(3, "Kishore"), 50000);
  map.put(new Employee(1, "Raju"), 70000);
  
  myMap = new TreeMap<> (map);
  
  System.out.println("Elements in myMap are");
  keys = myMap.keySet();
  iter = keys.iterator();
  
  while(iter.hasNext()){
   tmp = iter.next();
   System.out.print(tmp.id +" " + tmp.name +" ");
   System.out.println(myMap.get(tmp));
  } 
 }
}

Output
Elements in myMap are
2 Anand 95000
3 Kishore 50000
4 Krishna 15000
1 Raju 70000

1. Throws ClassCastException if the keys in m are not Comparable, or are not mutually comparable

import java.util.*;

class Employee{
 int id;
 String name;
 
 Employee(int id, String name){
  this.id = id;
  this.name = name;
 }
 
 public int compareTo(Employee o){
  return this.name.compareTo(o.name);
 }
 
}

import java.util.*;

class TreeMapConstructor3{
 public static void main(String args[]){
  TreeMap<Employee, Integer> myMap;
  Map<Employee, Integer> map;
  Set<Employee> keys;
  Iterator<Employee> iter; 
  Employee tmp;  
  
  map = new HashMap<> ();
  
  /* Insert data to myMap */
  map.put(new Employee(4, "Krishna"), 15000);
  map.put(new Employee(2, "Anand"), 95000);
  map.put(new Employee(3, "Kishore"), 50000);
  map.put(new Employee(1, "Raju"), 70000);
  
  myMap = new TreeMap<> (map);
  
  System.out.println("Elements in myMap are");
  keys = myMap.keySet();
  iter = keys.iterator();
  
  while(iter.hasNext()){
   tmp = iter.next();
   System.out.print(tmp.id +" " + tmp.name +" ");
   System.out.println(myMap.get(tmp));
  } 
 }
}

When you tries to run the program, JVM throws below error.

Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast
 to java.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1290)
        at java.util.TreeMap.put(TreeMap.java:538)
        at java.util.AbstractMap.putAll(AbstractMap.java:281)
        at java.util.TreeMap.putAll(TreeMap.java:327)
        at java.util.TreeMap.<init>(TreeMap.java:185)
        at TreeMapConstructor3.main(TreeMapConstructor3.java:19)

To make the program runs fine Employee class must implements the Comparable interface.

2. Throws NullPointerException if the specified map is null

import java.util.*;

class TreeMapConstructor3NullPointer{
 public static void main(String args[]){
  TreeMap<Integer, Integer> myMap;
  Map<Integer, Integer> map;
  
  map = null;  
  myMap = new TreeMap<> (map); 
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.putAll(TreeMap.java:313)
        at java.util.TreeMap.<init>(TreeMap.java:185)
        at TreeMapConstructor3NullPointer.main(TreeMapConstructor3NullPointer.ja
va:9)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment