Java Collections synchronizedNavigableSet() Method



Description

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

Declaration

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

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

Parameters

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

Return Value

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

Exception

NA

Getting Synchronized NavigableSet From a Unsynchronized NavigableSet of Integer Example

The following example shows the usage of Java Collection synchronizedNavigableSet(NavigableSet) method. We've created a NavigableSet object of Integer. Few entries are added and then using synchronizedNavigableSet(NavigableSet) method, we've retrieved the synchronized 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 synchronized navigable set NavigableSet<Integer> synset = Collections.synchronizedNavigableSet(navigableSet); System.out.println("Synchronized navigable set is :"+synset); } } 

Output

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

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

Getting Synchronized NavigableSet From a Unsynchronized NavigableSet of String Example

The following example shows the usage of Java Collection synchronizedNavigableSet(NavigableSet) method. We've created a NavigableSet object of String and String. Few entries are added and then using synchronizedNavigableSet(NavigableSet) method, we've retrieved the synchronized 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 synchronized navigable set NavigableSet<String> synset = Collections.synchronizedNavigableSet(navigableSet); System.out.println("Synchronized navigable set is :"+synset); } } 

Output

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

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

Getting Synchronized NavigableSet From a Unsynchronized NavigableSet of Object Example

The following example shows the usage of Java Collection synchronizedNavigableSet(NavigableSet) method. We've created a NavigableSet object of String and Student object. Few entries are added and then using synchronizedNavigableSet(NavigableSet) method, we've retrieved the synchronized 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 synchronized navigable set NavigableSet<Student> synset = Collections.synchronizedNavigableSet(navigableSet); System.out.println("Synchronized navigable set is :"+synset); } } 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.

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