Java.util.EnumSet.complementOf() Method



Description

The java.util.EnumSet.complementOf(EnumSet<E> s) method creates an enum set with the same element type as the specified enum set.It contains all the elements of this type that are not contained in the specified set.

Declaration

Following is the declaration for java.util.EnumSet.complementOf() method

public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)

Parameters

s − the enum set from whose complement to initialize this enum set.

Return Value

This method does not return any value.

Exception

NullPointerException − if s is null

Example

The following example shows the usage of java.util.EnumSet.complementOf() method.

package com.tutorialspoint;

import java.util.*;

public class EnumSetDemo {

   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {

   // create an EnumSet that has only Numbers.FOUR as element 
   EnumSet<Numbers> set1 = EnumSet.of(Numbers.FOUR);

   // print the set
   System.out.println("Set1:" + set1);

   // create a set2 which has all elements that set1 doesn't have
   EnumSet<Numbers> set2 = EnumSet.complementOf(set1);

   // print the updated set
   System.out.println("Set2:" + set2);

   }
}

Let us compile and run the above program, this will produce the following result −

Set1:[FOUR]
Set2:[ONE, TWO, THREE, FIVE]
java_util_enumset.htm
Advertisements