Sunday, December 29, 2024
Google search engine
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

Recent Comments