Java Collection Tutorial - Java Collections .unmodifiableSortedSet ( SortedSet <T> s)








Syntax

Collections.unmodifiableSortedSet(SortedSet <T> s) has the following syntax.

public static <T> SortedSet <T> unmodifiableSortedSet(SortedSet <T> s)

Example

In the following code shows how to use Collections.unmodifiableSortedSet(SortedSet <T> s) method.

import java.util.Collections;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
//from w w w .  j  a  va  2s . co  m
public class Main {
   public static void main(String[] s) {
      SortedSet<String>  set = new TreeSet<String> ();
      
      set.add("Welcome");
      set.add("to");
      set.add("java2s.com");
      
      System.out.println("Initial set value: "+set);
      
      // create unmodifiable sorted set
      Set<String> unmodsortset = Collections.unmodifiableSortedSet(set);

      // try to modify the sorted set
      unmodsortset.add("Hello");
   }
}

The code above generates the following result.