Java HashSet clear() 方法



描述

Java HashSet clear() 方法用於移除此集合中的所有元素。此呼叫返回後,集合將為空。

宣告

以下是 java.util.HashSet.clear() 方法的宣告。

public void clear()

引數

返回值

異常

清空整數 HashSet 示例

以下示例演示瞭如何使用 Java HashSet clear() 方法從 HashSet 中移除條目。我們建立了一個 Integer 型別的 HashSet 物件。然後使用 add() 方法添加了一些條目,並列印了集合。現在使用 clear() 方法清空集合,並再次列印。

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <Integer> newset = new HashSet <>();      

      // populate hash set
      newset.add(1); 
      newset.add(2);
      newset.add(3);  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);

      // clear the set
      newset.clear();

      // print the set
      System.out.println("Hash set values after clearing: "+ newset);
   }    
}

輸出

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

Hash set values: [1, 2, 3]
Hash set values after clearing: []

清空字串 HashSet 示例

以下示例演示瞭如何使用 Java HashSet clear() 方法從 HashSet 中移除條目。我們建立了一個 String 型別的 HashSet 物件。然後使用 add() 方法添加了一些條目,並列印了集合。現在使用 clear() 方法清空集合,並再次列印。

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <String> newset = new HashSet <>();      

      // populate hash set
      newset.add("Learning"); 
      newset.add("Easy");
      newset.add("Simply");  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);

      // clear the set
      newset.clear();

      // print the set
      System.out.println("Hash set values after clearing: "+ newset);
   }    
}

輸出

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

Hash set values: [Learning, Easy, Simply]
Hash set values after clearing: []

清空物件 HashSet 示例

以下示例演示瞭如何使用 Java HashSet clear() 方法從 HashSet 中移除條目。我們建立了一個 Student 物件型別的 HashSet 物件。然後使用 add() 方法添加了一些條目,並列印了集合。現在使用 clear() 方法清空集合,並再次列印。

package com.tutorialspoint;

import java.util.HashSet;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create hash set
      HashSet <Student> newset = new HashSet <>();      

      // populate hash set
      newset.add(new Student(1, "Julie")); 
      newset.add(new Student(2, "Robert"));
      newset.add(new Student(3, "Adam"));	  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newset);

      // clear the set
      newset.clear();

      // print the set
      System.out.println("Hash set values after clearing: "+ newset);
   }    
}
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 + " ]";
   }
}

輸出

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

Hash set values: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Hash set values after clearing: []
java_util_hashset.htm
廣告

© . All rights reserved.