NavigableMap is used for convenient navigation methods like lowerKey, floorKey, ceilingKey, and higherKey, along with this popular navigation method. It will take key-value pair as input
We can create a navigable map by using the following syntax:
NavigableMap<key_datatype, value_datatype> data= new TreeMap<key_datatype, value_datatype>();
where
- data is the input data.
- key_datatype refers to the key type element.
- value_datatype refers to the value type element.
synchronizedNavigableMap() Method will return the synchronized, which is a thread-safe navigable map backed by the specified navigable map.
Syntax:
public static <Key,Value> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<Key,Value> data)
where data is the navigable map which will be wrapped into a synchronized navigable map.
Return Type: The synchronizedNavigableMap() method returns a synchronized view of the specified Navigable Map.
Example 1: Create a synchronized navigable map using string elements
Java
import java.util.*; public class GFG1 { // main method public static void main(String[] args) { // create a NavigableMap NavigableMap<String, String> data = new TreeMap<String, String>(); // add data values data.put( "1" , "java" ); data.put( "2" , "python" ); data.put( "3" , "php" ); data.put( "4" , "html/js" ); // create synchronized NavigableMap from the created // map(data) Map<String, String> syn = Collections.synchronizedNavigableMap(data); System.out.println(syn); } } |
{1=java, 2=python, 3=php, 4=html/js}
Example 2:
Java
import java.util.*; public class GFG1 { // main method public static void main(String[] args) { // create a NavigableMap NavigableMap<Integer, Integer> data = new TreeMap<Integer, Integer>(); // add data values data.put( 1 , 34 ); data.put( 2 , 45 ); data.put( 3 , 74 ); data.put( 4 , 41 ); data.put( 5 , 4 ); data.put( 6 , 40 ); // create synchronized NavigableMap // from the created map(data) Map<Integer, Integer> syn = Collections.synchronizedNavigableMap(data); System.out.println(syn); } } |
{1=34, 2=45, 3=74, 4=41, 5=4, 6=40}