EnumSet.of(K, K, K, K, K): This method is available in java.util.EnumSet class of Java.
Syntax:
<K> EnumSet<K> java.util.EnumSet.of(K e1, K e2, K e3, K e4, K e5)
This method takes five arguments. This method creates an enum set initially containing the specified elements.
Parameters: Five parameters are required for this method.
e1: an element that this set is to contain initially.
e2: another element that this set is to contain initially.
e3: another element that this set is to contain initially.
e4: another element that this set is to contain initially.
e5: another element that this set is to contain initially.
Returns: an enum set initially containing the specified elements.
Throws:
NullPointerException - if any parameters are null
Approach 1: When no exception
Java
import java.util.EnumSet;public class EnumSetof6 {public enum Colour {RED, GREEN, YELLOW, ORANGE};public static void main(String[] args) {EnumSet<Colour> enumSet;Colour e5 = Colour.RED;enumSet = EnumSet.of(Colour.GREEN,Colour.ORANGE, Colour.RED, Colour.YELLOW, e5);System.out.println(enumSet);}}
Output:
[RED, GREEN, YELLOW, ORANGE]
Approach 2: NullPointerException
Java
import java.util.EnumSet;public class EnumSetof6 {public enum Colour {RED, GREEN, YELLOW, ORANGE};public static void main(String[] args) {EnumSet<Colour> enumSet;Colour e5 = null;enumSet = EnumSet.of(Colour.GREEN,Colour.ORANGE, Colour.RED, Colour.YELLOW, e5);System.out.println(enumSet);}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "e" is null at java.base/java.util.EnumSet.typeCheck(EnumSet.java:397) at java.base/java.util.RegularEnumSet.add(RegularEnumSet.java:162) at java.base/java.util.RegularEnumSet.add(RegularEnumSet.java:36) at java.base/java.util.EnumSet.of(EnumSet.java:319)
No comments:
Post a Comment