Saturday, 3 May 2014

EnumSet : of( E e1, E e2, E e3, E e4, E e5 )

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

import java.util.*;

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

Output
Elements in mySet1 are
[MON, TUE, WED, THU, SUN]

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

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

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:301)
        at EnumSetOf5Null.main(EnumSetOf5Null.java:11)

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment