Java集合synchronizedNavigableSet()方法



描述

Java集合synchronizedNavigableSet()方法用於返回一個由指定的可導航集合支援的同步(執行緒安全)的可導航集合。

宣告

以下是java.util.Collections.synchronizedNavigableSet()方法的宣告。

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

引數

s − 這是要“包裝”到同步可導航集合中的可導航集合。

返回值

  • 方法呼叫返回指定可導航集合的同步檢視。

異常

從非同步整數可導航集合獲取同步可導航集合示例

以下示例演示了Java集合synchronizedNavigableSet(NavigableSet)方法的使用。我們建立了一個Integer型別的NavigableSet物件。添加了一些條目,然後使用synchronizedNavigableSet(NavigableSet)方法,我們檢索了可導航集合的同步版本並列印了該可導航集合。

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);
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

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

從非同步字串可導航集合獲取同步可導航集合示例

以下示例演示了Java集合synchronizedNavigableSet(NavigableSet)方法的使用。我們建立了一個String型別的NavigableSet物件。添加了一些條目,然後使用synchronizedNavigableSet(NavigableSet)方法,我們檢索了map的同步版本並列印了該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);
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

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

從非同步物件可導航集合獲取同步可導航集合示例

以下示例演示了Java集合synchronizedNavigableSet(NavigableSet)方法的使用。我們建立了一個String和Student物件型別的NavigableSet物件。添加了一些條目,然後使用synchronizedNavigableSet(NavigableSet)方法,我們檢索了map的同步版本並列印了該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;
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Synchronized navigable set is :[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
廣告
© . All rights reserved.