当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collections synchronizedNavigableSet()用法及代码示例


Java 集合中的 synchronizedNavigableSet() 方法用于获取具有给定可导航集的线程安全可导航集。

用法:

public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> 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<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);
    }
}
输出
[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<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);
    }
}
输出
[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<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);
    }
}
输出
[4532, 7058, 7859]



相关用法


注:本文由纯净天空筛选整理自manojkumarreddymallidi大神的英文原创作品 Java Collections synchronizedNavigableSet() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。