Saturday, 3 May 2014

EnumSet : of( E e1, E e2 )

public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
Creates an enum set initially containing the specified elements.

import java.util.*;

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

Output
Elements in mySet1 are
[MON, SUN]

1. throws NullPointerException if any parameters are null
import java.util.*;

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

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.EnumSet.typeCheck(EnumSet.java:378)
        at java.util.RegularEnumSet.add(RegularEnumSet.java:160)
        at java.util.RegularEnumSet.add(RegularEnumSet.java:36)
        at java.util.EnumSet.of(EnumSet.java:325)
        at EnumSetOf2Null.main(EnumSetOf2Null.java:10)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment