Friday, 6 June 2014

TreeMap : clear() : Removes all the mappings from this map

public void clear()
Removes all of the mappings from this map.

import java.util.*;

class TreeMapClear{
  public static void main(String args[]){
  TreeMap<Integer, String> myMap1 = new TreeMap<> ();
  
  /* Add data to the myMap1 */
  myMap1.put(1, "First");
  myMap1.put(2, "Second");
  myMap1.put(3, "Third");
  myMap1.put(4, "Fourth");
  
  System.out.println("Data in myMap1 is");
  System.out.println(myMap1);
  
  System.out.println("\nCalling clear method of myMap1");
  myMap1.clear();
  
  System.out.println("\nData in myMap1 is");
  System.out.println(myMap1);
  }
}

Output
Data in myMap1 is
{1=First, 2=Second, 3=Third, 4=Fourth}

Calling clear method of myMap1

Data in myMap1 is
{}




Prevoius                                                 Next                                                 Home

TreeMap : ceilingKey(K key) : get the ceiling key specific to this key

public K ceilingKey(K key)
Returns the least key greater than or equal to the given key, or null if there is no such key.

import java.util.*;

class TreeMapCeilingKey{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Integer ceilingKey;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  ceilingKey = myMap.ceilingKey(6);
  System.out.print("Ceiling Key for 6 is ");
  System.out.println(ceilingKey);
  
  ceilingKey = myMap.ceilingKey(100);
  System.out.print("Ceiling Key for 100 is ");
  System.out.println(ceilingKey);
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Ceiling Entry for 6 is 10
Ceiling Entry for 100 is null

1. Throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.
import java.util.*;

class TreeMapCeilingKeyNullPointer{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Integer ceilingKey;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Ceiling Key for null is ");
  ceilingKey = myMap.ceilingKey(null);
  System.out.println(ceilingKey);
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Ceiling Key for null is
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1290)
        at java.util.TreeMap.getCeilingEntry(TreeMap.java:397)
        at java.util.TreeMap.ceilingKey(TreeMap.java:755)
        at TreeMapCeilingKeyNullPointer.main(TreeMapCeilingKeyNullPointer.java:2
1)



Prevoius                                                 Next                                                 Home

TreeMap : ceilingEntry : Get the ceiling entry specific to this key

public Map.Entry<K,V> ceilingEntry(K key)
Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

import java.util.*;

class TreeMapCeilingEntry{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Map.Entry<Integer, String> ceilingEntry;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  ceilingEntry = myMap.ceilingEntry(6);
  System.out.print("Ceiling Entry for 6 is ");
  System.out.println(ceilingEntry);
  
  ceilingEntry = myMap.ceilingEntry(100);
  System.out.print("Ceiling Entry for 100 is ");
  System.out.println(ceilingEntry);
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Ceiling Entry for 6 is 10=c
Ceiling Entry for 100 is null

1. Throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys

import java.util.*;

class TreeMapCeilingEntryNullPointer{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  Map.Entry<Integer, String> ceilingEntry;
  
  myMap = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap.put(10, "c");
  myMap.put(1, "a");
  myMap.put(15, "d");
  myMap.put(5, "b");
  myMap.put(99, "e");

  System.out.println("Elements in myMap are");
  System.out.println(myMap);
  
  System.out.println("Ceiling Entry for null is ");
  ceilingEntry = myMap.ceilingEntry(null);
  System.out.println(ceilingEntry);
 }
}

Output
Elements in myMap are
{1=a, 5=b, 10=c, 15=d, 99=e}
Ceiling Entry for null is
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1290)
        at java.util.TreeMap.getCeilingEntry(TreeMap.java:397)
        at java.util.TreeMap.ceilingEntry(TreeMap.java:744)
        at TreeMapCeilingEntryNullPointer.main(TreeMapCeilingEntryNullPointer.ja
va:21)



Prevoius                                                 Next                                                 Home

Thursday, 5 June 2014

TreeMap (SortedMap m)

public TreeMap(SortedMap<K, ? extends V> m)
Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. 

import java.util.*;

class TreeMapConstructor4{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap1;
  TreeMap<Integer, String> myMap2;
  SortedMap<Integer, String> subMap;
  
  myMap2 = new TreeMap<> ();
  
  /* Add Elements to myMap2 */
  myMap2.put(10, "c");
  myMap2.put(1, "a");
  myMap2.put(15, "d");
  myMap2.put(5, "b");
  myMap2.put(99, "e");
  
  subMap = myMap2.subMap(5, 20);
  
  System.out.println("Elements in myMap2 are");
  System.out.println(myMap2);
  
  System.out.println("Elements in subMap are");
  System.out.println(subMap);
  
  myMap1 = new TreeMap<> (subMap);
  
  System.out.println("Elements in myMap1 are");
  System.out.println(myMap1);
  
 }
}

Output
Elements in myMap2 are
{1=a, 5=b, 10=c, 15=d, 99=e}
Elements in subMap are
{5=b, 10=c, 15=d}
Elements in myMap1 are
{5=b, 10=c, 15=d}

1. Throws NullPointerException if the specified map is null
import java.util.*;

class TreeMapConstructor4NullPointer{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap1;
  SortedMap<Integer, String> subMap;
  
  subMap = null;
  
  myMap1 = new TreeMap<> (subMap);
  
  System.out.println("Elements in myMap1 are");
  System.out.println(myMap1);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.<init>(TreeMap.java:198)
        at TreeMapConstructor4NullPointer.main(TreeMapConstructor4NullPointer.ja
va:10)




Prevoius                                                 Next                                                 Home

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

TreeMap (Comparator comparator)

public TreeMap(Comparator<? super K> comparator)
Constructs a new, empty tree map, ordered according to the given comparator.

class Employee1{
 Integer number;
 String name;
 
 Employee1(int number, String  name){
  this.number = number;
  this.name = name;
 } 
 
 public String toString(){
  return number + " " + name;
 }
}

import java.util.Comparator;
class EmployeeIdComparator implements Comparator<Employee1>{

 public int compare(Employee1 e1, Employee1 e2){
  return e1.number.compareTo(e2.number);
 }
 
 public String toString(){
  return "EmployeeIdComparator";
 }
}

import java.util.*;

class TreeMapConstructor2{
 public static void main(String args[]){
  TreeMap<Employee1, Integer> myMap;
  myMap = new TreeMap<> (new EmployeeIdComparator());
  
  /* Insert data to myMap */
  myMap.put(new Employee1(4, "Krishna"), 15000);
  myMap.put(new Employee1(2, "Anand"), 95000);
  myMap.put(new Employee1(3, "Kishore"), 50000);
  myMap.put(new Employee1(1, "Raju"), 70000);
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);  
 }
}

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


Prevoius                                                 Next                                                 Home

public TreeMap()

public TreeMap()
Constructs a new, empty tree map, using the natural ordering of its keys. All the keys inserted into the TreeMap must implements the Comparable interface.

import java.util.*;

class TreeMapConstructor1{
 public static void main(String args[]){
  TreeMap<Integer, String> myMap;
  myMap = new TreeMap<> ();
  
  /* Insert data to myMap */
  myMap.put(1, "a");
  myMap.put(7, "d");
  myMap.put(2, "b");
  myMap.put(6, "d");
  myMap.put(3, "c");
  myMap.put(5, "d");
  myMap.put(4, "d");
  
  System.out.println("Elements in myMap are");
  System.out.println(myMap);  
 }
}

Output
Elements in myMap are
{1=a, 2=b, 3=c, 4=d, 5=d, 6=d, 7=d}

As you observe the output, TreeMap prints the output in natural ordering of the key, which is integer here.

Prevoius                                                 Next                                                 Home