public
static <E extends Enum<E>> EnumSet<E>
copyOf(Collection<E> c)
Creates
an enum set initialized from the specified collection.
import java.util.*; class EnumSetCopyOfCollection{ enum Day{ MON,TUE,WED,THU, FRI,SAT,SUN; } public static void main(String args[]){ Collection<Day> coll1 = new ArrayList<> (); /* Add Elements to the Collection */ coll1.add(Day.MON); coll1.add(Day.TUE); coll1.add(Day.WED); coll1.add(Day.THU); coll1.add(Day.FRI); EnumSet<Day> mySet1 = EnumSet.copyOf(coll1); System.out.println("Elements in coll1 are"); System.out.println(coll1); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); } }
Output
Elements in coll1 are [MON, TUE, WED, THU, FRI] Elements in mySet1 are [MON, TUE, WED, THU, FRI]
1. throws IllegalArgumentException if c is not an EnumSet instance and contains no elements
import java.util.*; class EnumSetCopyOfCollectionIllegal{ enum Day{ MON,TUE,WED,THU, FRI,SAT,SUN; } public static void main(String args[]){ Collection<Day> coll1 = new ArrayList<> (); EnumSet<Day> mySet1 = EnumSet.copyOf(coll1); System.out.println("Elements in coll1 are"); System.out.println(coll1); System.out.println("Elements in mySet1 are"); System.out.println(mySet1); } }
Output
Exception in thread "main" java.lang.IllegalArgumentException: Collection is empty at java.util.EnumSet.copyOf(EnumSet.java:166) at EnumSetCopyOfCollectionIllegal.main(EnumSetCopyOfCollectionIllegal.java:12)
2. throws
NullPointerException if c is null
import java.util.*; class EnumSetCopyOfCollectionNull{ enum Day{ MON,TUE,WED,THU, FRI,SAT,SUN; } public static void main(String args[]){ Collection<Day> coll1 = null; EnumSet<Day> mySet1 = EnumSet.copyOf(coll1); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.util.EnumSet.copyOf(EnumSet.java:165) at EnumSetCopyOfCollectionNull.main(EnumSetCopyOfCollectionNull.java:12)
No comments:
Post a Comment