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