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.


    public static EnumSet complementOf(EnumSet es);


  • 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.


// 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 {

    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);


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


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.