Collections.synchronizedSortedMap(SortedMap) in Java

Collections.synchronizedSortedMap(SortedMap): This method is available in java.util.Collections class of Java.

Syntax:

<E1, E2> SortedMap<E1, E2> java.util.Collections.synchronizedSortedMap(SortedMap<E1, E2> m)

This method takes one argument. This method returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

Parameters: One parameter is required for this method.

m: the sorted map to be "wrapped" in a synchronized sorted map.

Returns: a synchronized view of the specified sorted map.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class CollectionssynchronizedSortedMap {
    public static void main(String[] args) {

        SortedMap<String, Integer> sortedMap = new
TreeMap<String, Integer>();

        sortedMap.put("Hello", 10);
        sortedMap.put("Java", 56);
        sortedMap.put("C++", 5);

        System.out.println(Collections.synchronizedSortedMap(sortedMap));

    }
}

Output:

{C++=5, Hello=10, Java=56}


No comments:

Post a Comment