Java Utility Library

Java Collections - synchronizedNavigableMap() Method



The java.util.Collections.synchronizedNavigableMap() method returns a synchronized (thread-safe) navigable map backed by the specified navigable map.

Syntax

public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m)

Here, K and V are the type of key and value respectively maintained by the navigable map.


Parameters

m Specify the navigable map to be "wrapped" in a synchronized navigable map.

Return Value

Returns a synchronized view of the specified navigable map.

Exception

NA.

Example:

In the example below, the java.util.Collections.synchronizedNavigableMap() method returns a synchronized view of the given navigable map.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a NavigableMap object
    NavigableMap<Integer, String> MyMap = new TreeMap<Integer, String>();

    //populating MyMap
    MyMap.put(101, "John");
    MyMap.put(102, "Marry");
    MyMap.put(103, "Kim");

    //printing the map
    System.out.println("MyMap contains: " + MyMap); 

    //creating an synchronized view of the navigable map
    NavigableMap NewMap = Collections.synchronizedNavigableMap(MyMap);

    //printing the synchronized navigable map
    System.out.println("NewMap contains: " + NewMap); 
  }
}

The output of the above code will be:

MyMap contains: {101=John, 102=Marry, 103=Kim}
NewMap contains: {101=John, 102=Marry, 103=Kim}

❮ Java.util - Collections