Java Collections unmodifiableNavigableSet() Method



Description

The Java Collections unmodifiableNavigableSet() method is used to return a immutable (thread-safe) navigable set backed by the specified navigable set.

Declaration

Following is the declaration for java.util.Collections.unmodifiableNavigableSet() method.

public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)

Parameters

s − This is the navigable set to be "wrapped" in a immutable navigable set.

Return Value

  • The method call returns a immutable view of the specified navigable set.

Exception

NA

Getting Immutable NavigableSet From a Mutable NavigableSet of Integer Example

The following example shows the usage of Java Collection unmodifiableNavigableSet(NavigableSet) method. We've created a NavigableSet object of Integer. Few entries are added and then using unmodifiableNavigableSet(NavigableSet) method, we've retrieved the immutable version of navigable set and printed the navigable set.

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeSet;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create navigable set
      NavigableSet<Integer> navigableSet = new TreeSet<Integer>();

      // populate the navigable set
      navigableSet.add(1); 
      navigableSet.add(2);
      navigableSet.add(3);

      // create a immutable navigable set
      NavigableSet<Integer> immutableSet = Collections.unmodifiableNavigableSet(navigableSet);

      System.out.println("Immutable navigable set is :"+immutableSet);
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Immutable navigable set is :[1, 2, 3]

Getting Immutable NavigableSet From a Mutable NavigableSet of String Example

The following example shows the usage of Java Collection unmodifiableNavigableSet(NavigableSet) method. We've created a NavigableSet object of String and String. Few entries are added and then using unmodifiableNavigableSet(NavigableSet) method, we've retrieved the immutable version of map and printed the map.

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeSet;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create navigable set
      NavigableSet<String> navigableSet = new TreeSet<String>();

      // populate the navigable set
      navigableSet.add("TP"); 
      navigableSet.add("IS");
      navigableSet.add("BEST");

      // create a immutable navigable set
      NavigableSet<String> immutableSet = Collections.unmodifiableNavigableSet(navigableSet);

      System.out.println("Immutable navigable set is :"+immutableSet);
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Immutable navigable set is :[BEST, IS, TP]

Getting Immutable NavigableSet From a Mutable NavigableSet of Object Example

The following example shows the usage of Java Collection unmodifiableNavigableSet(NavigableSet) method. We've created a NavigableSet object of String and Student object. Few entries are added and then using unmodifiableNavigableSet(NavigableSet) method, we've retrieved the immutable version of map and printed the map.

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeSet;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create navigable set
      NavigableSet<Student> navigableSet = new TreeSet<Student>();

      // populate the navigable set
      navigableSet.add(new Student(1, "Julie")); 
      navigableSet.add(new Student(2, "Robert"));
      navigableSet.add(new Student(3, "Adam"));

      // create a immutable navigable set
      NavigableSet<Student> immutableSet = Collections.unmodifiableNavigableSet(navigableSet);

      System.out.println("Immutable navigable set is :"+immutableSet);
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Immutable navigable set is :[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
Advertisements