Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clear
enumset
java
communitycreator

What is the EnumSet.clear method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

EnumSet is similar to Set except that EnumSet only contains the Enum type as elements. Also, all the elements must be from the same Enum type.

For a detailed review of module EnumSet, refer here.

The EnumSet.clear() method can be used to delete all the elements present in the EnumSet object.

Syntax

public void clear()

About the method

This method neither takes any argument nor returns any value. It just removes all the memory occupied by the elements of that Enumset object.

Code

import java.util.EnumSet;
class isEmpty {
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
public static void main( String args[] ) {
EnumSet<Size> set = EnumSet.allOf(Size.class);
System.out.println("The set is " + set);
set.clear();
System.out.println("After using set.clear()\nThe set is " + set);
}
}

In the above code,

  • Line 11: Imported the EnumSet class.

  • Line 33: We have created an Enum with the name Size.

  • Line 77: Created a new EnumSet object using the allOf method. We have passed the Size.class as an argument to the allOf method, which will return an EnumSet object which contains all the elements of Size Enum.

  • Line 99: We have used the clear method to delete all elements present in the set as it will make the set empty.

RELATED TAGS

clear
enumset
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring