Open In App

Java Collections emptyNavigableSet() Method with Examples

Last Updated : 03 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The emptyNavigableSet() 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 can store unique elements.

Syntax:

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

Parameters: This will take no parameters.

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

Example 1:

Java




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


Output

[]

Example 2: In this program, we are going to create an empty navigable set and add elements to the set. This will return an error.

Java




// Java program to show the exception while
// using Collections emptyNavigableSet() 
// Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an emptyNavigableSet
        Set<String> data
            = Collections.<String>emptyNavigableSet();
        
        // 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:10)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads