Saturday, September 6, 2025
HomeLanguagesJavaJava Collections emptyNavigableMap() Method with Examples

Java Collections emptyNavigableMap() Method with Examples

The Java Collections emptyNavigableMap() method is used to get a map with no elements. A Navigable map is a data structure that can hold elements with key-value pairs.

Syntax:

public static final <Key,Value> SortedMap<Key,Value> emptyNavigableMap()  

where,

  • key is the key element
  • value is the value element

Parameters: This method will take no parameters.

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

Example 1:

Java




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


Output

{}

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

Java




// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap<String, String> data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put("1", "ojaswi");
        data.put("2", "ramya");
        data.put("3", "deepu");
  
        // display
        System.out.println(data);
    }
}


Output:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
    at GFG.main(GFG.java:10)

Example 3:

Java




// Java program to show the error while using
// Collections emptyNavigableMap() Method
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an empty navigable map
        SortedMap<Integer, Integer> data
            = Collections.emptyNavigableMap();
        
        // add 3 elements
        data.put(1, 34);
        data.put(2, 45);
        data.put(3, 56);
  
        // display
        System.out.println(data);
    }
}


Output:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
    at GFG.main(GFG.java:10)
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS