📅  最后修改于: 2023-12-03 15:31:57.064000             🧑  作者: Mango
在Java中,HashSet是一种无序的、不重复的集合。remove()是HashSet中的一个方法,用于从HashSet中删除元素。
boolean remove(Object o)
如果HashSet中存在该元素,则删除成功并返回true。否则,不会删除任何元素并返回false。
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> mySet = new HashSet<String>();
mySet.add("apple");
mySet.add("banana");
mySet.add("orange");
System.out.println("HashSet before remove: " + mySet);
boolean isRemoved = mySet.remove("banana");
System.out.println("Was banana removed? " + isRemoved);
System.out.println("HashSet after remove: " + mySet);
}
}
/* 输出:
HashSet before remove: [orange, banana, apple]
Was banana removed? true
HashSet after remove: [orange, apple]
*/