97
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
Output:
Empty Set: []
Example 2
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
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)
Next TopicJava Collections Class