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[])
    {

        // 创建一个空的AbstractCollection
        AbstractCollection<String>
            abs = new TreeSet<String>();

      // 使用add()方法向集合中添加元素
        abs.add("欢迎"); 
        abs.add("到");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
        abs.add("TreeSet");

      // 显示集合
        System.out.println("Collection: " + abs);

      // 使用remove()方法移除元素
        abs.remove("Geeks");       
        abs.remove("4");
        abs.remove("TreeSet");

      // 移除后显示集合
        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[])
    {

        // 创建一个空的AbstractCollection
        AbstractCollection<String>
            abs = new LinkedList<String>();

      // 使用add()方法向集合中添加元素
        abs.add("欢迎");
        abs.add("到");
        abs.add("Geeks");
        abs.add("4");
        abs.add("Geeks");
        abs.add("LinkedList");

      // 显示集合
        System.out.println("Collection: " + abs);

      // 使用remove()方法移除元素
        abs.remove("Geeks");       
        abs.remove("4");
        abs.remove("LinkedList");

      // 移除后显示集合
        System.out.println("New Collection: " + abs);
    }
}
Collection: [Welcome, To, Geeks, 4, Geeks, LinkedList]
New Collection: [Welcome, To, Geeks]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程