Wednesday, 19 March 2014

Remove Element from the Set

boolean remove(Object o)
    Removes the specified element from this set if it is present. Returns true if the Element removed else false.

import java.util.*;
class SetRemove{
 public static void main(String args[]){
  Set<Integer> mySet =new TreeSet<> ();
  
  /* Add elements to the set */
  mySet.add(1);
  mySet.add(-10);
  mySet.add(100);
  mySet.add(12);
  mySet.add(14);
  
  /* Display the set */
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
  
  /* Removing -10 from the set */
  mySet.remove(-10);
  
  System.out.println("\nElements in mySet After removing -10 are");
  System.out.println(mySet);  
 }
}

Output

Elements in mySet are
[-10, 1, 12, 14, 100]

Elements in mySet After removing -10 are
[1, 12, 14, 100]
remove throws ClassCastException if the type of the specified element is incompatible with this set.

class Employee{
 int number;
 
 Employee(int number){
  this.number = number;
 }
}



import java.util.*;
class SetClassCastRemove{
 public static void main(String args[]){
  Set<Employee> mySet = new TreeSet<> ();
  
  Employee e1 = new Employee(1);
  boolean flag = mySet.remove(e1);
  
  System.out.println("Is Employee Removed " + flag);
 }
}
When you tries too run the program run time throws the “ClassCastException”.
Output

Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
        at java.util.TreeMap.getEntry(Unknown Source)
        at java.util.TreeMap.remove(Unknown Source)
        at java.util.TreeSet.remove(Unknown Source)
        at SetClassCastRemove.main(SetClassCastRemove.java:7)


To make the program compiles fine, Make the Employee class to implement Comparable interface.

class Employee implements Comparable{
 int number;
 
 public int compareTo(Object emp){
  if(emp==null)
   return 0;
  
  Employee e = (Employee) emp;
  
  if(this.number == e.number)
   return 0;
   
  return (this.number - e.number);
 }
 
 Employee(int number){
  this.number = number;
 } 
 
 public String toString(){
  return "Employee " + number;
 }
 
}

Output

Is Employee Removed false


remove() throws NullPointerException if the specified element is null and this set does not permit null elements.

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

TreeSet doesn't permit null elements. So trying to remove the null elements from the TreeSet cause the NullPointerException.

Output

Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.getEntry(Unknown Source)
        at java.util.TreeMap.remove(Unknown Source)
        at java.util.TreeSet.remove(Unknown Source)
        at SetRemoveNull.main(SetRemoveNull.java:5)


remove throws UnsupportedOperationException if the remove operation is not supported by this set.

import java.util.*;
class SetUnSupportedRemove{
 public static void main(String args[]){
  Set<Integer> mySet = new HashSet<> ();
  Set<Integer> unModifySet;
  /* Add Elements to the set */
  mySet.add(10);
  mySet.add(20);
  mySet.add(30);
  
  /* Getting unmodifiable version of the set mySet */
  unModifySet = Collections.unmodifiableSet(mySet);
  
  /* Displaying the Elements of the sets */
  System.out.println("Elements in mySet are");  
  System.out.println(mySet);
  System.out.println("Elements in unModifySet are");
  System.out.println(unModifySet);
  
  /* Remove integer from unmodifiable set, which 
     throws UnsupportedOperationException */  
  unModifySet.remove(20);
 }
}

Output

Elements in mySet are
[20, 10, 30]
Elements in unModifySet are
[20, 10, 30]
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableCollection.remove(Unknown Source)
        at SetUnSupportedRemove.main(SetUnSupportedRemove.java:22)
 Above program, tries to remove a element from the un modifiable set, So UnsupportedOperationException thrown.

 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment