Collections.unmodifiableMap(Map) in Java

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

Syntax:

<E1, E2> Map<E1, E2> java.util.Collections.unmodifiableMap(Map<? extends E1, ? extends E2> m)

This method takes one argument. This method returns an unmodifiable view of the specified map.

Parameters: One parameter is required for this method.

m: the map for which an unmodifiable view is to be returned.

Returns: an unmodifiable view of the specified map.

Exceptions: NA

Approach

Java

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

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

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

        map.put("Hello", 1);
        map.put("Java", 2);
        map.put("C++", 3);

        System.out.println(Collections.unmodifiableMap(map));

    }
}

Output:

{Java=2, C++=3, Hello=1}


No comments:

Post a Comment