The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection.
Syntax:
AbstractCollection.remove(Object O)
Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection.
Return Value: This method returns True if the element specified in the parameter is initially present in the collection and is successfully removed otherwise it returns False.
Below program illustrate the Java.util.AbstractCollection.remove() method:
Program 1:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new TreeSet<String>();
abs.add( "Welcome" );
abs.add( "To" );
abs.add( "Geeks" );
abs.add( "4" );
abs.add( "Geeks" );
abs.add( "TreeSet" );
System.out.println( "Collection: " + abs);
abs.remove( "Geeks" );
abs.remove( "4" );
abs.remove( "TreeSet" );
System.out.println( "New Collection: " + abs);
}
}
|
Output:
Collection: [4, Geeks, To, TreeSet, Welcome]
New Collection: [To, Welcome]
Program 2:
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs = new LinkedList<String>();
abs.add( "Welcome" );
abs.add( "To" );
abs.add( "Geeks" );
abs.add( "4" );
abs.add( "Geeks" );
abs.add( "LinkedList" );
System.out.println( "Collection: " + abs);
abs.remove( "Geeks" );
abs.remove( "4" );
abs.remove( "LinkedList" );
System.out.println( "New Collection: " + abs);
}
}
|
Output:
Collection: [Welcome, To, Geeks, 4, Geeks, LinkedList]
New Collection: [Welcome, To, Geeks]