boolean remove(Object o)
描述 (Description)
remove(Object o)方法用于从该集合中删除指定的元素(如果存在)。
声明 (Declaration)
以下是java.util.HashSet.remove()方法的声明。
public boolean remove(Object o)
参数 (Parameters)
o - 这是要从此集中删除的对象(如果存在)。
返回值 (Return Value)
如果set包含指定的元素,则方法调用返回'true'。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.util.HashSet.remove()的用法
package com.iowiki;
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]