當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.HashSet.remove()用法及代碼示例


描述

這個remove(Object o)方法用於從該集合中刪除指定的元素(如果存在)。

聲明

以下是聲明java.util.HashSet.remove()方法。

public boolean remove(Object o)

參數

o�這是要從該集合中刪除的對象(如果存在)。

返回值

如果集合包含指定的元素,則方法調用返回 'true'。

異常

NA

示例

下麵的例子展示了 java.util.HashSet.remove() 的用法

package com.tutorialspoint;

import java.util.*;

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

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

      System.out.println("Values before remove:"+newset);

      // remove "Easy" from set
      boolean isremoved = newset.remove("Easy"); 

      System.out.println("Return value after remove:"+isremoved); 

      System.out.println("Values after remove:"+newset);     
   }    
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Values before remove:[Learning, Simply, Easy]
Return value after remove:true
Values after remove:[Learning, Simply]

相關用法


注:本文由純淨天空篩選整理自 java.util.HashSet.remove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。