Java Collections emptySortedSet() 方法



描述

Java Collections emptySortedSet() 方法用於獲取空的有序集合(不可變)。此集合是可序列化的。

宣告

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

 
public static final <T> SortedSet<T> emptySortedSet()

引數

返回值

異常

獲取整數的空有序集合示例

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

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.SortedSet;

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

      System.out.println("Created empty immutable sorted set: "+emptySortedSet);

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

輸出

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

Created empty immutable sorted 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 Collection emptySortedSet() 方法獲取字串的空有序集合。我們使用 emptySortedSet() 方法建立了一個空的有序集合,然後嘗試向其中新增一些元素,這會導致異常。

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.SortedSet;

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

      System.out.println("Created empty immutable sorted set: "+emptySortedSet);

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

輸出

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

Created empty immutable sorted 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 Collection emptySortedSet() 方法獲取 Student 物件的空有序集合。我們使用 emptySortedSet() 方法建立了一個空的有序集合,然後嘗試向其中新增一些元素,這會導致異常。

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.SortedSet;

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

      System.out.println("Created empty immutable sorted set: "+emptySortedSet);

      // try to add elements
      emptySortedSet.add(new Student(1, "Julie"));
      emptySortedSet.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 sorted 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
廣告

© . All rights reserved.