Home » Java programming language

Java Collections unmodifiableSortedMap() Method with Example

Collections Class unmodifiableSortedMap() method: Here, we are going to learn about the unmodifiableSortedMap() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 08, 2020

Collections Class unmodifiableSortedMap() method

  • unmodifiableSortedMap() method is available in java.util package.
  • unmodifiableSortedMap() method is used to get a non-modifiable view of the given Set (set).
  • unmodifiableSortedMap() method is a static method, it is accessible with the class name and if we try to access the method with the class object then also we will not get any error.
  • unmodifiableSortedMap() method does not throw an exception at the time of returning an unmodifiable view of the given set.

Syntax:

    public static SortedMap unmodifiableSortedMap(SortedMap sm);

Parameter(s):

  • SortedMap sm – represents the sorted map object for which a non-modifiable view is to be retrieved.

Return value:

The return type of this method is SortedMap, it returns an unmodifiable view of the given sorted map.

Example:

// Java program to demonstrate the example 
// of SortedMap unmodifiableSortedMap() 
// method of Collections

import java.util.*;

public class UnmodifiableSortedMapOfCollections {
    public static void main(String args[]) {
        // Instantiates a sorted map object
        SortedMap < Integer, String > tm = new TreeMap < Integer, String > ();

        // By using put() method is to add
        // objects in a treemap 
        tm.put(10, "C");
        tm.put(20, "C++");
        tm.put(30, "JAVA");
        tm.put(40, "PHP");
        tm.put(50, "SFDC");

        // Display TreeMap
        System.out.println("TreeMap: " + tm);

        //By using unmodifiableSortedMap() method is to
        //represent the treemap in unmodifiable view
        Map um = Collections.unmodifiableSortedMap(tm);

        // we will get an exception when we try 
        // to add an object in unmodifiable map

        /* um.put(60,"COBOL")*/
    }
}

Output

TreeMap: {10=C, 20=C++, 30=JAVA, 40=PHP, 50=SFDC}


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.