EnumSet.of(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)
This method takes three arguments. This method creates an enum set initially containing the specified elements.
Parameters: Three 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.
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 EnumSetof4 {public enum Colour {RED, GREEN, YELLOW, ORANGE};public static void main(String[] args) {EnumSet<Colour> enumSet;Colour e3 = Colour.RED;enumSet = EnumSet.of(Colour.GREEN, Colour.ORANGE, e3);System.out.println(enumSet);}}
Output:
[RED, GREEN, ORANGE]
Approach 2: NullPointerException
Java
import java.util.EnumSet;public class EnumSetof4 {public enum Colour {RED, GREEN, YELLOW, ORANGE};public static void main(String[] args) {EnumSet<Colour> enumSet;Colour e3 = null;enumSet = EnumSet.of(Colour.GREEN, Colour.ORANGE, e3);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:263)
No comments:
Post a Comment