Open In App

Java Collections synchronizedNavigableSet() Method with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The synchronizedNavigableSet() method in Java collections is used to get the thread-safe navigable set with the given navigable set.

Syntax:

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

Parameters:

  • set is the input navigable set.

Return: It will return the synchronized navigable set from the given input (navigable set).

Exception: It will not raise any exception.

Example:

Java




// Java program to add elements
// to the Navigable set and convert
// them into the synchronized 
// navigable set with string data
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an navigable tree set
        NavigableSet<String> data = new TreeSet<>();
  
        // add elements into the set
        data.add("sravan-it");
        data.add("manoj-cse");
        data.add("sai-cse");
        data.add("vignesh-it");
  
        // get the synchronized navigable 
        // set from the above set
        Set<String> final1
            = Collections.synchronizedNavigableSet(data);
  
        // display
        System.out.println(final1);
    }
}


Output

[manoj-cse, sai-cse, sravan-it, vignesh-it]

Example 2:

Java




// Java program to add elements to the Navigable
// set and convert into the synchronized 
// navigable set with integer data
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an navigable tree set
        NavigableSet<Integer> data = new TreeSet<>();
  
        // add elements into the set
        data.add(7058);
        data.add(4511);
        data.add(7859);
        data.add(4532);
  
        // get the synchronized navigable 
        // set from the above set
        Set<Integer> final1
            = Collections.synchronizedNavigableSet(data);
  
        // display
        System.out.println(final1);
    }
}


Output

[4511, 4532, 7058, 7859]

Example 3: 

Java




// Java program to remove an item
// from the synchronized navigable
// set
import java.util.*;
  
public class GFG {
    // main method
    public static void main(String[] args)
    {
        // create an navigable tree set
        NavigableSet<Integer> data = new TreeSet<>();
  
        // add elements into the set
        data.add(7058);
        data.add(4511);
        data.add(7859);
        data.add(4532);
  
        // get the synchronized navigable 
        // set from the above set
        Set<Integer> final1
            = Collections.synchronizedNavigableSet(data);
  
        // remove 4511 element
        final1.remove(4511);
        // display
        System.out.println(final1);
    }
}


Output

[4532, 7058, 7859]


Last Updated : 03 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads