Home » Java programming language

Java EnumSet complementOf() Method with Example

EnumSet Class complementOf() method: Here, we are going to learn about the complementOf() method of EnumSet Class with its syntax and example.
Submitted by Preeti Jain, on February 13, 2020

EnumSet Class complementOf() method

  • complementOf() method is available in java.util package.
  • complementOf() method is used to contain all the elements of this EnumSet that are complement in the given EnumSet.
  • complementOf() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • complementOf() method may throw an exception at the time of returning complement EnumSet.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public static EnumSet complementOf(EnumSet es);

Parameter(s):

  • EnumSet es – represents the another enum set from whose complement to assign this enum set.

Return value:

The return type of this method is EnumSet, it returns complement enum set of the given enum set.

Example:

// Java program is to demonstrate the example of
// complementOf(EnumSet es) method of EnumSet

import java.util.*;

public class ComplementOfEnumSet {
    // Initialize a enum variable
    // with some constants

    public enum Colors {
        RED,
        BLUE,
        GREEN,
        PURPLE,
        YELLOW
    };

    public static void main(String[] args) {
        // Here , we are creating two EnumSet
        // First EnumSet is intiatize with some 
        // values and Second EnumSet is empty
        EnumSet < Colors > es = EnumSet.of(Colors.PURPLE);
        EnumSet < Colors > complement_es = null;

        // Display EnumSet
        System.out.println("EnumSet (es): " + es);

        // By using complementOf() method is to 
        // contain all of the elements that does
        // not exists in the given EnumSet es
        complement_es = EnumSet.complementOf(es);

        // Display EnumSet
        System.out.println("EnumSet.complementOf(es): " + complement_es);
    }
}

Output

EnumSet (es): [PURPLE]
EnumSet.complementOf(es): [RED, BLUE, GREEN, YELLOW]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.