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

File Name: CollectionsEmptySetExample1.java

Output:

Empty Set: []

Collections.emptySet() returns an immutable empty set (Collections.EMPTY_SET) of type Set<String>. The set emptySet cannot be modified; any attempt to add, remove, or modify elements will result in an UnsupportedOperationException. Printing emptySet displays an empty set ([]), indicating its immutable and empty state.

Example 2

File Name: CollectionsEmptySetExample2.java

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

File Name: CollectionsEmptySetExample2.java

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)

Getting Empty Set of Object Example

In Java, the Collections class provides a convenient way to obtain an immutable empty set of objects using the emptySet() method. This method returns a set that is guaranteed to be empty and immutable, meaning it cannot be modified after it is created.

File Name: EmptyObjectSetExample.java

Output:

Size of empty object set: 0
After clear operation, size of empty object set: 0
Class of empty object set: java.util.Collections$EmptySet

Advantages of Collections.emptySet() Method

  1. Immutable Empty Set: Collections.emptySet() returns an immutable (read-only) empty set. It ensures that the set cannot be modified, providing thread safety and preventing accidental changes.
  2. Avoids NullPointerException: It eliminates the need for null checks when returning empty sets. Instead of returning null, which requires additional checks in client code, we can return an empty set using Collections.emptySet().
  3. Convenient and Readable: Using Collections.emptySet() provides a clear and concise way to obtain an empty set instance. It enhances code readability and makes the intent of returning an empty set explicit.
  4. Performance Benefits: Creating and returning a singleton empty set instance (Collections.emptySet()) is more efficient than creating new empty sets every time they are needed. This is because immutable objects can be cached and reused.
  5. Thread Safety: Since the set returned by Collections.emptySet() is immutable, it can be safely accessed and used concurrently in a multi-threaded environment without the need for synchronization.

Disadvantages of Collections.emptySet() Method

  1. Cannot Modify: The set returned by Collections.emptySet() is immutable. While this is generally an advantage for thread safety and clarity, it means that you cannot add, remove, or change elements in the set. Any attempt to modify the set will result in an UnsupportedOperationException.
  2. Limited Use Cases: It can only be used to create an empty set and not for creating a set with initial elements. If you need a set with elements, you would have to use other methods like HashSet or TreeSet.
  3. Potential Confusion: Developers unfamiliar with the method might not immediately recognize its immutability. This could lead to confusion if they attempt to modify the returned set and encounter unexpected UnsupportedOperationExceptions.





Latest Courses