Java Collections synchronizedNavigableMap​() Method with Examples
Last Updated :
28 Dec, 2021
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 {
public static void main(String[] args)
{
NavigableMap<String, String> data
= new TreeMap<String, String>();
data.put( "1" , "java" );
data.put( "2" , "python" );
data.put( "3" , "php" );
data.put( "4" , "html/js" );
Map<String, String> syn
= Collections.synchronizedNavigableMap(data);
System.out.println(syn);
}
}
|
Output
{1=java, 2=python, 3=php, 4=html/js}
Example 2:
Java
import java.util.*;
public class GFG1 {
public static void main(String[] args)
{
NavigableMap<Integer, Integer> data
= new TreeMap<Integer, Integer>();
data.put( 1 , 34 );
data.put( 2 , 45 );
data.put( 3 , 74 );
data.put( 4 , 41 );
data.put( 5 , 4 );
data.put( 6 , 40 );
Map<Integer, Integer> syn
= Collections.synchronizedNavigableMap(data);
System.out.println(syn);
}
}
|
Output
{1=34, 2=45, 3=74, 4=41, 5=4, 6=40}
Share your thoughts in the comments
Please Login to comment...