📌  相关文章
📜  Java中的 AbstractCollection remove() 方法及示例

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

Java中的 AbstractCollection remove() 方法及示例

Java AbstractCollectionremove(Object O)方法是从 Collection 中删除特定元素。

句法:

AbstractCollection.remove(Object O)

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

返回值:如果参数中指定的元素最初存在于集合中并且成功删除,则此方法返回True ,否则返回False

下面的程序说明了Java.util.AbstractCollection.remove() 方法:

程序 1

// Java code to illustrate remove()
  
import java.util.*;
import java.util.AbstractCollection;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty AbstractCollection
        AbstractCollection
            abs = new TreeSet();
  
        // Use add() method to add
        // elements into the Collection
        abs.add("Welcome");
        abs.add("To");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
        abs.add("TreeSet");
  
        // Displaying the Collection
        System.out.println("Collection: " + abs);
  
        // Removing elements using remove() method
        abs.remove("Geeks");
        abs.remove("4");
        abs.remove("TreeSet");
  
        // Displaying the Collection after removal
        System.out.println("New Collection: " + abs);
    }
}
输出:
Collection: [4, Geeks, To, TreeSet, Welcome]
New Collection: [To, Welcome]

方案二:

// Java code to illustrate remove()
  
import java.util.*;
import java.util.AbstractCollection;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty AbstractCollection
        AbstractCollection
            abs = new LinkedList();
  
        // Use add() method to add
        // elements into the Collection
        abs.add("Welcome");
        abs.add("To");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
        abs.add("LinkedList");
  
        // Displaying the Collection
        System.out.println("Collection: " + abs);
  
        // Removing elements using remove() method
        abs.remove("Geeks");
        abs.remove("4");
        abs.remove("LinkedList");
  
        // Displaying the Collection after removal
        System.out.println("New Collection: " + abs);
    }
}
输出:
Collection: [Welcome, To, Geeks, 4, Geeks, LinkedList]
New Collection: [Welcome, To, Geeks]