Saturday, 3 May 2014

EnumSet : of( E e )

public static <E extends Enum<E>> EnumSet<E> of(E e)
Creates an enum set initially containing the specified element.

import java.util.*;

class EnumSetOf{
 enum Day{
  MON,TUE,WED,THU,
  FRI,SAT,SUN;
 }
 
 public static void main(String args[]){  
  EnumSet<Day> mySet1 = EnumSet.of(Day.SUN);
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1);
 }
}

Output
Elements in mySet1 are
[SUN]

1. throws NullPointerException if e is null
import java.util.*;

class EnumSetOfNull{
 enum Day{
  MON,TUE,WED,THU,
  FRI,SAT,SUN;
 }
 
 public static void main(String args[]){  
  EnumSet<Day> mySet1 = EnumSet.of(null);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.EnumSet.of(EnumSet.java:204)
        at EnumSetOfNull.main(EnumSetOfNull.java:10)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment