Wednesday, July 3, 2024
HomeLanguagesJavaHow to Remove an Element from Collection using Iterator Object in Java?

How to Remove an Element from Collection using Iterator Object in Java?

Collection in Java is a set of interfaces like List, Set, and Queue. An iterator in Java is used to iterate or traverse through the elements of the Collection. There are three types of iterator in Java namely, Enumerator, Iterator, and ListIterator. 

The two ways of removing an element from Collections using Iterator :

  1. Using Iterator
  2. Using ListIterator

Approach 1: Using Iterator

  • A List is created and elements are added to the list using the add() method.
  • The Iterator object is used to iterate over the elements of the list using the hasNext() and next() methods.
  • An if the condition is used within the while loop and when the condition is satisfied, the particular element is removed using the remove() method.
  • When the entire list is traversed again the element which was removed is no longer present in the list.

Java




// Java program to Remove an Element from 
// Collection using Iterator 
  
import java.util.ArrayList;
import java.util.Iterator;
class IteratorDemo 
{
    public static void main(String[] args) 
    {
        ArrayList<Integer> l = new ArrayList<Integer>();
        
        for(int i=0;i<=50;i=i+5)
        {
            l.add(i);
        }
        
        Iterator<Integer> itr = l.iterator();
        
        System.out.println("List before removal");
        
        for(int i=0;i<l.size();i++)
        {
            System.out.print(l.get(i)+" ");
        }
          
        while(itr.hasNext())
        {
            if(itr.next()%2==1)
                itr.remove();
        }
      
        System.out.println("\nList after removal");
        
        for(int i=0;i<l.size();i++)
        {
            System.out.print(l.get(i)+" ");
        }
    }
  
}


Output

List before removal
0 5 10 15 20 25 30 35 40 45 50 
List after removal
0 10 20 30 40 50

Approach 2: Using ListIterator

  • A list is created and elements are added to the list using the add() method.
  • The ListIterator object is used to iterate over the elements of the list using the hasNext() and next() methods.
  • An if condition is used within the while loop and when the condition is satisfied, the particular element is removed using the remove() method.
  • When the entire list is traversed again the element which was removed is no longer present in the list.

Java




// Java program to Remove an Element from
// Collection using ListIterator
  
import java.util.ArrayList;
import java.util.ListIterator;
  
public class ListIteratorDemo {
    public static void main(String[] args)
    {
        ArrayList<String> l = new ArrayList<String>();
  
        l.add("January");
        l.add("February");
        l.add("March");
        l.add("April");
        l.add("May");
        l.add("June");
        l.add("July");
        l.add("August");
  
        ListIterator<String> itr = l.listIterator();
  
        System.out.println("List before removal");
  
        for (int i = 0; i < l.size(); i++)
            System.out.print(l.get(i) + " ");
  
        while (itr.hasNext()) {
            if (itr.next().equals("March")) {
                itr.remove();
            }
        }
  
        System.out.println("\nList after removal");
  
        for (int i = 0; i < l.size(); i++)
            System.out.print(l.get(i) + " ");
    }
}


Output

List before removal
January February March April May June July August 
List after removal
January February April May June July August

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments