Saturday, 3 May 2014

EnumSet : copyOf (EnumSet s)

public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)
Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).

import java.util.*;

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

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

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

class EnumSetCopyOfEnumSetNull{
 public static void main(String args[]){  
  EnumSet<Day> mySet;
  mySet = EnumSet.copyOf(null);
 }
}


Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.EnumSet.copyOf(EnumSet.java:146)
        at EnumSetCopyOfEnumSetNull.main(EnumSetCopyOfEnumSetNull.java:6)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment