📜  Java中的HashSet remove()方法

📅  最后修改于: 2022-05-13 01:54:30.021000             🧑  作者: Mango

Java中的HashSet remove()方法

HashSet remove() 方法用于从 HashSet 中删除特定元素。注意它只在JDK 1.2 及之前版本,在JDK 1 和JDK1.1 版本之前会抛出编译错误。

句法:

HashSet.remove(Object O)

参数:参数O是HashSet的类型,指定要从HashSet中移除的元素。

返回值:布尔真假

示例 1:

Java
// Java code to illustrate
// HashSet.remove() method
// over String Elements
 
// Importing required classes
import java.util.*;
 
// Main class
// HashSet demo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty HashSet
        // Declaring object of string type
        HashSet set = new HashSet();
 
        // Adding custom input elements into the Set
        // using add() method
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("For");
        set.add("Geeks");
 
        // Displaying the HashSet(object elements)
        System.out.println("HashSet: " + set);
 
        // Removing elements
        // using remove() method
        set.remove("Geeks");
        set.remove("For");
        set.remove("Welcome");
 
        // Now displaying the HashSet after removal
        // of elements from it
        System.out.println(
            "HashSet after removing elements: " + set);
    }
}


Java
// Java code to illustrate remove()
// method of Hashset class
// over Integer Elements
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty HashSet
        // Declaring object of integer type
        HashSet set = new HashSet();
 
        // Adding custom input elements into the Set
        // using add() method
        set.add(5);
        set.add(3);
        set.add(1);
        set.add(4);
        set.add(3);
 
        // Displaying the HashSet(object elements)
        System.out.println("HashSet: " + set);
 
        // Removing elements
        // using remove() method
        set.remove(3);
        set.remove(1);
 
        // Now displaying the HashSet after removal
        // of elements from it
        System.out.println(
            "HashSet after removing elements: " + set);
    }
}


输出
HashSet: [Geeks, For, Welcome, To]
HashSet after removing elements: [To]

示例 2:

Java

// Java code to illustrate remove()
// method of Hashset class
// over Integer Elements
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty HashSet
        // Declaring object of integer type
        HashSet set = new HashSet();
 
        // Adding custom input elements into the Set
        // using add() method
        set.add(5);
        set.add(3);
        set.add(1);
        set.add(4);
        set.add(3);
 
        // Displaying the HashSet(object elements)
        System.out.println("HashSet: " + set);
 
        // Removing elements
        // using remove() method
        set.remove(3);
        set.remove(1);
 
        // Now displaying the HashSet after removal
        // of elements from it
        System.out.println(
            "HashSet after removing elements: " + set);
    }
}
输出
HashSet: [1, 3, 4, 5]
HashSet after removing elements: [4, 5]