Collections.unmodifiableSortedMap(SortedMap) in Java

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

Syntax:

<E1, E2> SortedMap<E1, E2> java.util.Collections.unmodifiableSortedMap(SortedMap<E1, ? extends E2> m)

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

Parameters: One parameter is required for this method.

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

Returns: an unmodifiable view of the specified sorted map.

Exceptions: NA

Approach

Java

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

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

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

        map.put("Hello", 12);
        map.put("Java", 5);
        map.put("C++", 56);

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

    }
}

Output:

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


No comments:

Post a Comment