Saturday 19 April 2014

TreeSet : add : add Element to TreeSet

public boolean add(E e)
Adds the specified element to this set if it is not already present. Return true if this set did not already contain the specified element.

import java.util.*;
class TreeSetAdd{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to the set */
  mySet.add(10);
  mySet.add(20);
  
  System.out.println("Elements in the set are");
  System.out.println(mySet);
 }
}

Output
Elements in the set are
[10, 20]

1. throws ClassCastException if the specified object cannot be compared with the elements currently in this set
If you want to add any data to TreeSet then the data must implements the comparable interface, other wise ClassCastException thrown.

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

import java.util.*;
class TreeSetAddClassCast{
 public static void main(String args[]){
  TreeSet<Employee> mySet = new TreeSet<> ();
  
  Employee e1 = new Employee(1);
  Employee e2 = new Employee(2);
  
  /* Add Elements to the set */
  mySet.add(e1);
  mySet.add(e2);
  
  System.out.println("Elements in the set are");
  System.out.println(mySet);
 }
}

When you tries to run the program, compiler 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:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.TreeSet.add(TreeSet.java:255)
        at TreeSetAddClassCast.main(TreeSetAddClassCast.java:10)

To make the program run fine, Employee class must implement the Comparator interface.

class Employee implements Comparable{
 Integer number;
 
 Employee(int number){
  this.number = number;
 } 
 
 public String toString(){
  return "Employee " + number;
 }
 
 public int compareTo(Object obj){
  if(obj == null)
   throw new NullPointerException();
  if(!(obj instanceof Employee))
   throw new IllegalArgumentException();
   
  Employee e1 = (Employee) obj;
  
  return this.number.compareTo(e1.number);
 }
}

Output
Elements in the set are
[Employee 1, Employee 2]

2. throws NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements.

import java.util.*;
class TreeSetAddNullPointer{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();

  /* Add null to the set */
  mySet.add(null);
 }
}

Output

Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.put(TreeMap.java:531)
        at java.util.TreeSet.add(TreeSet.java:255)
        at TreeSetAddNullPointer.main(TreeSetAddNullPointer.java:7)





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment