Collections.synchronizedMap(Map) in Java

Collections.synchronizedMap(Map): This method is available in java.util.Collections class of Java.

Syntax:

<E1, E2> Map<E1, E2> java.util.Collections.synchronizedMap(Map<E1, E2> m)

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

Parameters: One parameter is required for this method.

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

Returns: a synchronized view of the specified map.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

        Map<String, Integer> map = new HashMap<String,
Integer>();

        map.put("Hello", 12);
        map.put("Java", 23);
        map.put("C++", 25);
        System.out.println(Collections.synchronizedMap(map));

    }
}

Output:

{Java=23, C++=25, Hello=12}


No comments:

Post a Comment