當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。