Java Utility Library

Java Collections - synchronizedNavigableSet() Method



The java.util.Collections.synchronizedNavigableSet() method returns a synchronized (thread-safe) navigable set backed by the specified navigable set.

Syntax

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

Here, T is the type of element in the navigable set.


Parameters

s Specify the navigable set to be "wrapped" in a synchronized navigable set.

Return Value

Returns a synchronized view of the specified navigable set.

Exception

NA.

Example:

In the example below, the java.util.Collections.synchronizedNavigableSet() method returns a synchronized view of the given navigable set.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a NavigableSet object
    NavigableSet<Integer> MySet = new TreeSet<Integer>();

    //populating the set
    MySet.add(10);
    MySet.add(20);
    MySet.add(30);
    MySet.add(40);

    //printing the set
    System.out.println("MySet contains: " + MySet); 

    //creating an synchronized view of the navigable set
    NavigableSet NewSet = Collections.synchronizedNavigableSet(MySet);

    //printing the synchronized navigable set
    System.out.println("NewSet contains: " + NewSet); 
  }
}

The output of the above code will be:

MySet contains: [10, 20, 30, 40]
NewSet contains: [10, 20, 30, 40]

❮ Java.util - Collections