Java AbstractCollection remove()方法及示例

Java AbstractCollection remove()方法及示例

Java AbstractCollection的remove(Object O)方法是为了从一个集合中删除一个特定的元素。

语法

AbstractCollection.remove(Object O)

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

返回值: 如果参数中指定的元素最初存在于集合中并被成功移除,该方法返回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<String>
            abs = new TreeSet<String>();
  
        // 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]

程序 2:

// 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<String>
            abs = new LinkedList<String>();
  
        // 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]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程