Java Collections emptySet() Method

The emptySet() method of Java Collections class is used to get the Set that has no elements. These empty Set are immutable in nature.

Syntax

Following is the declaration of emptySet() method:

Parameter

This method does not accept any parameter.

Returns

The emptySet() method returns an empty immutable Set.

Exceptions

NA

Compatibility Version

Java 1.5 and above

Example 1

Test it Now

Output:

Empty Set: []

Example 2

Test it Now

Output:

Created empty immutable Set: []
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractCollection.add(AbstractCollection.java:267)
	at myPackage.CollectionsEmptySetExample2.main(CollectionsEmptySetExample2.java:10)

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractCollection.add(AbstractCollection.java:267)
	at myPackage.CollectionsEmptySetExample3.main(CollectionsEmptySetExample3.java:8)





Latest Courses