Home »
Java programming language
How to remove an entry from a Collection in Java?
Removing an entry/element from Collection: Here, we are going to learn how to remove an entry from a Collection in Java programing language? We are also learning about the differences between the remove() method of Collection interface and remove() method of Iterator interface?
Submitted by Preeti Jain, on August 04, 2019
Removing an entry from a Collection
- As we know, we can remove an entry from a Collection in three ways.
- By using remove(Object obj) method of Collection
- By using remove(int index) method of List
- By using remove() method of Iterator
- Collection interface adds a method remove(Object obj) is used to remove a specified element from a Collection.
- List interface adds another remove(int index) method is used to remove an object at the specified index in the method.
- Iterator interface also adds remove() method is used to remove the current object from a Collection.
Now, we will see how remove(Object obj) method of Collection differs from remove() method of Iterator?
remove(Object obj) method of Collection interface
Example:
// Java program to demonstrate the behavior of
// remove() method of Collection
import java.util.*;
class RemoveCollectionMethod {
public static void main(String[] args) {
Collection collection = new LinkedList();
collection.add(10);
collection.add(20);
collection.add(30);
collection.add(40);
collection.add(50);
System.out.println("Current LinkedList:" + collection);
collection.remove(30);
System.out.println(" LinkedList:" + collection);
}
}
Output
E:\Programs>javac RemoveCollectionMethod.java
E:\Programs>java RemoveCollectionMethod
Current LinkedList:[10, 20, 30, 40, 50]
LinkedList:[10, 20, 40, 50]
Now, we will see how remove() method of Iterator differs from remove(Object obj) method of Collection?
remove() method of Iterator interface
- This method is available in java.util package.
- remove() method of Iterator removes the current object or element in the Collection.
- In case of remove() method, we can't remove the specified element or random element in the middle directly but without iteration.
-
The syntax of this remove() method of Iterator interface is given below:
void remove(){}
- The return type of remove() method is void so it does not return anything.
- This method throws an exception IllegalStateException if this method is called before next() method is called.
- When we iterate, Let suppose we are traversing a list and remove only a few elements based on logic and if we use Iterator remove() method then we will not get any exception there.
Example:
// Java program to demonstrate the behavior of
// remove() method of Iterator
import java.util.*;
class RemoveIteratorMethod {
public static void main(String[] args) {
// Creating a Set interface object
Set set = new HashSet();
// By using add() method to add few elements in set
set.add("Java");
set.add("Python");
set.add("C++");
set.add("Ruby");
set.add("C");
// Creating an object of Iterator interface
Iterator itr = set.iterator();
// loop for traversing an element
while (itr.hasNext()) {
String str = (String) itr.next();
if (str.equals("C"))
itr.remove();
}
// Display elements of Set interface
System.out.println("The final list of Iterator :" + set);
}
}
Output
E:\Programs>javac RemoveIteratorMethod.java
E:\Programs>java RemoveIteratorMethod
The final list of Iterator :[Ruby, Python, C++, Java]