Java Collections emptyNavigableMap() Method with Examples
Last Updated :
03 Jan, 2022
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
SortedMap<String, String> data
= Collections.emptyNavigableMap();
System.out.println(data);
}
}
|
Example 2: In this program, we will create an empty navigable map and add elements to the map. This will return an error.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
SortedMap<String, String> data
= Collections.emptyNavigableMap();
data.put( "1" , "ojaswi" );
data.put( "2" , "ramya" );
data.put( "3" , "deepu" );
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
SortedMap<Integer, Integer> data
= Collections.emptyNavigableMap();
data.put( 1 , 34 );
data.put( 2 , 45 );
data.put( 3 , 56 );
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)
Share your thoughts in the comments
Please Login to comment...