Java Utility Library

Java Collections - emptyNavigableSet() Method



The java.util.Collections.emptyNavigableSet() method returns an empty navigable set (immutable). This set is serializable.

Syntax

public static <E> NavigableSet<E> emptyNavigableSet()

Here, E is the type of element, if there were any, in the set.


Parameters

No parameter is required.

Return Value

Returns the empty navigable set.

Exception

NA.

Example:

In the example below, the java.util.Collections.emptyNavigableSet() method is used to create an empty navigable set.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an empty NavigableSet
    NavigableSet<Integer> MySet = Collections.emptyNavigableSet();

    //print the content of the set
    System.out.println("MySet contains: " + MySet);

    //populating the set
    //as the set is immutable, 
    //the exception is thrown 
    MySet.add(10);
    MySet.add(20);
    MySet.add(30);
    MySet.add(40);
    MySet.add(50);

    //print the content of the set
    System.out.println("MySet contains: " + MySet);
  }
}

The output of the above code will be:

MySet contains: []

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1060)
    at MyClass.main(MyClass.java:14)

❮ Java.util - Collections