VOOZH about

URL: https://www.geeksforgeeks.org/java/java-collections-emptysortedset-method-with-examples/

⇱ Java Collections emptySortedSet() Method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Collections emptySortedSet() Method with Examples

Last Updated : 3 Jan, 2022

The emptySortedSet() method of Java Collections is used to get the set that has no elements. This method is used in set collection. A set is a data structure that stores unique elements in a sorted manner.

Syntax:

public static final <T> Set<T> emptySortedSet() 

Parameters: This will take no parameters.

Return Type: It will return an empty set that is immutable.

Example 1:

Java program to create an empty sorted set


Output
[]

Example 2:

In this program, we are going to create an empty sorted set and add elements to the set. This code will return an error.

Output:

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.util.Collections$UnmodifiableCollection.add(Collections.java:1057)
 at GFG.main(GFG.java:9)
Comment