Java集合 synchronizedNavigableSet() 方法和示例
Java集合中的synchronizedNavigableSet()方法用于获取具有给定可导航集的线程安全可导航集。
句法:
public static NavigableSet synchronizedNavigableSet(NavigableSet set)
参数:
- set是输入的可导航集。
返回:它将从给定的输入(可导航集)返回同步的可导航集。
异常:它不会引发任何异常。
例子:
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 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 final1
= Collections.synchronizedNavigableSet(data);
// display
System.out.println(final1);
}
}
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 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 final1
= Collections.synchronizedNavigableSet(data);
// display
System.out.println(final1);
}
}
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 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 final1
= Collections.synchronizedNavigableSet(data);
// remove 4511 element
final1.remove(4511);
// display
System.out.println(final1);
}
}
输出
[manoj-cse, sai-cse, sravan-it, vignesh-it]
示例 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 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 final1
= Collections.synchronizedNavigableSet(data);
// display
System.out.println(final1);
}
}
输出
[4511, 4532, 7058, 7859]
示例 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 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 final1
= Collections.synchronizedNavigableSet(data);
// remove 4511 element
final1.remove(4511);
// display
System.out.println(final1);
}
}
输出
[4532, 7058, 7859]