Java 集合 singleton() 方法



描述

Java 集合 singleton(T) 方法用於返回僅包含指定物件的不可變集。

宣告

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

public static <T> Set<T> singleton(T o)

引數

o − 這是要儲存在返回集合中的唯一物件。

返回值

方法呼叫返回僅包含指定物件的不可變集。

異常

獲取整數單例集示例

以下示例演示了 Java 集合 singleton(T) 方法的使用。我們建立了一個包含一些整數的 List 物件,並列印了原始列表。使用 singleton(T) 方法,我們刪除了給定值的列表元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // remove 2 from this collection
      list.removeAll(Collections.singleton(2));
      System.out.println("Final collection value: "+list);
   }
}

輸出

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

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [1, 3, 4, 5]

獲取字串單例集示例

以下示例演示了 Java 集合 singleton(T) 方法的使用。我們建立了一個包含一些字串的 List 物件,並列印了原始列表。使用 singleton(T) 方法,我們刪除了給定值的列表元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint"));

      System.out.println("Initial collection value: " + list);
      // remove "to" from this collection
      list.removeAll(Collections.singleton("to"));
      System.out.println("Final collection value: "+list);
   }
}

輸出

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

Initial collection value: [Welcome, to, Tutorialspoint]
Final collection value: [Welcome, Tutorialspoint]

獲取物件單例集示例

以下示例演示了 Java 集合 singleton(T) 方法的使用。我們建立了一個包含一些 Student 物件的 List 物件,並列印了原始列表。使用 singleton(T) 方法,我們刪除了給定值的列表元素,然後列印了更新後的列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Initial collection value: " + list);
      // remove Robert from this collection
      list.removeAll(Collections.singleton(new Student(2, "Robert")));
      System.out.println("Final collection value: "+list);
   }
}
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 + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

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

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Final collection value: [[ 1, Julie ], [ 3, Adam ]]
java_util_collections.htm
廣告

© . All rights reserved.