Wednesday 19 March 2014

Add an Element to the Set

boolean add(E e)
    Adds the specified element to this set if it is not already present. Returns true if the element is added else false.

import java.util.*;
class SetAddEle{
 public static void main(String args[]){
  Set<Integer> mySet = new HashSet<> ();
  
  /* 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
[20, 10]


Depends on the implementation, some sets accept null and some not accept null. HashSet accepts null, where as Tree set can't. 

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

 Output

Elements in the set are
[null]


As you observe the output, HashSet accepts null.

TreeSet can't accept the null, If program tries to add a null to the TreeSet then “NullPointerException” thrown at run time.

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

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

C:\java SetAddEle
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at SetAddEle.main(SetAddEle.java:7)

add() throws UnsupportedOperationException , if the add operation is not supported by this set.

import java.util.*;
class SetUnSupportedAdd{
 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);
  
  /* Getting unmodifiable version of the set mySet */
  unModifySet = Collections.unmodifiableSet(mySet);
  
  /* Adding integer variable to mySet */
  mySet.add(30);
  
  /* 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);
  
  /* Adding integer to unmodifiable set, which 
     throws UnsupportedOperationException */  
  unModifySet.add(40);
 }
}

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.add(Unknown Source)
        at SetUnSupportedAdd.main(SetUnSupportedAdd.java:24)

As you observe the program.
    unModifySet = Collections.unmodifiableSet(mySet);
Above statement returns the unmodifiable version of the Set, so trying to add any data to the un modifiable set cause the UnsupportedOperationException at run time.

Add() throws ClassCastException if the class of the specified element prevents it from being added to this set.

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



import java.util.*;
class ClassCastEx{
 public static void main(String args[]){
  Set<Employee> mySet = new TreeSet<> ();
  
  mySet.add(new Employee(1));
  mySet.add(new Employee(2));
  
  System.out.println("Employees in the set are");
  System.out.println(mySet);
 }
}


When you tries to run the ClassCastEx, ClassCastException thrown at run time.

Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at ClassCastEx.main(ClassCastEx.java:6)

To make the program run fine, Employee class must implement the 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;
 }
 
}

Now run the file ClassCastEx.
Output

Employees in the set are
[Employee 1, Employee 2]
 
throws IllegalArgumentException, if some property of the specified element prevents it from being added to this set.

import java.util.*;
class IllegalArgumentEx{
 public static void main(String args[]){
  SortedSet<Integer> mySet = new TreeSet<> ();
  SortedSet<Integer> subSet;
  
  /* Add Elements to mySet */
  mySet.add(10);
  mySet.add(20);
  mySet.add(15);
  mySet.add(12);
  mySet.add(16);
  
  Integer from =new Integer(10);
  Integer to = new Integer(15);
  
  /* Get the subset in the range of 10 to 15 */
  subSet = mySet.subSet(from, to);
  
  /* Display Elements */
  System.out.println(subSet);
  
  subSet.add(14);
  System.out.println(subSet);
  
  /* Add 16, which is out of range between 10 to 15 */
  subSet.add(16);
 }
}

Output

[10, 12]
[10, 12, 14]
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
        at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at IllegalArgumentEx.main(IllegalArgumentEx.java:27)

 As you observe the program, subSet able to accept the elements in the range of 10 to 15. But when the program tries to add 16, which is out of Range, then run time throws IllegalArgumentException.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment