Friday, July 24, 2026
HomeLanguagesJavaJava Collections emptySortedSet() Method with Examples

Java Collections emptySortedSet() Method with Examples

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

Java




import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty sorted set
        Set<String> data
            = Collections.<String>emptySortedSet();
        
        // display
        System.out.println(data);
    }
}


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.

Java




import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty sorted set
        Set<String> data
            = Collections.<String>emptySortedSet();
        
        // add 3 elements
        data.add("ojaswi");
        data.add("ramya");
        data.add("deepu");
  
        // display
        System.out.println(data);
    }
}


Output:

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

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS