Java集合emptyNavigableSet()方法



描述

Java集合emptyNavigableSet()方法用於獲取空的不可變導航集。此集合是可序列化的。

宣告

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

public static final <T> NavigableSet<T> emptyNavigableSet()

引數

返回值

異常

獲取空整數導航集示例

以下示例演示瞭如何使用Java集合emptyNavigableSet()方法獲取空的整數導航集。我們使用emptyNavigableSet()方法建立了一個空的導航集,然後嘗試向其中新增一些元素,這將導致異常。

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty navigable set    
      NavigableSet<Integer> emptyNavigableSet = Collections.emptyNavigableSet();

      System.out.println("Created empty immutable navigable set: "+emptyNavigableSet);

      // try to add elements
      emptyNavigableSet.add(1);
      emptyNavigableSet.add(2);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。由於列表是不可變的,因此會丟擲異常。

Created empty immutable navigable set: []
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1060)
	at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:15)

獲取空字串導航集示例

以下示例演示瞭如何使用Java集合emptyNavigableSet()方法獲取空的字串導航集。我們使用emptyNavigableSet()方法建立了一個空的導航集,然後嘗試向其中新增一些元素,這將導致異常。

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty navigable set    
      NavigableSet<String> emptyNavigableSet = Collections.emptyNavigableSet();

      System.out.println("Created empty immutable navigable set: "+emptyNavigableSet);

      // try to add elements
      emptyNavigableSet.add("A");
      emptyNavigableSet.add("B");
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。由於列表是不可變的,因此會丟擲異常。

Created empty immutable navigable set: []
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1060)
	at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:15)

獲取空物件導航集示例

以下示例演示瞭如何使用Java集合emptyNavigableSet()方法獲取空的Student物件導航集。我們使用emptyNavigableSet()方法建立了一個空的導航集,然後嘗試向其中新增一些元素,這將導致異常。

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty navigable set    
      NavigableSet<Student> emptyNavigableSet = Collections.emptyNavigableSet();

      System.out.println("Created empty immutable navigable set: "+emptyNavigableSet);

      // try to add elements
      emptyNavigableSet.add(new Student(1, "Julie"));
      emptyNavigableSet.add(new Student(2, "Robert"));
   }    
}
class 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 + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。由於列表是不可變的,因此會丟擲異常。

Created empty immutable navigable set: []
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1060)
	at com.tutorialspoint.CollectionsDemo.main(CollectionsDemo.java:15)
java_util_collections.htm
廣告