Iterators in Java from scratch

Article by: Manish Methani

Last Updated: October 25, 2021 at 8:04am IST
2 mins 51 sec read

Java Iterator is used to iterate through all the elements in the List. An Iterator is used to go through each element of Collections in Java easily. Let's continue with the example we followed in ArrayList tutorial.

The Methods Declared by Iterator

Sr.No. Method & Description
1

boolean hasNext( )

Returns true if there are more elements. Otherwise, returns false.

2

Object next( )

Returns the next element. Throws NoSuchElementException if there is not a next element.

3

void remove( )

Removes the current element. Throws IllegalStateException if an attempt is

made to call remove( ) that is not preceded by a call to next( ).

Example

In this example, we created two Strings array named things & more things. Two Lists named list1 & list2. Task this program will perform is firstly it prints all the list1 elements then it checks list2 whether list2 contains list1 elements and if yes remove them from list1 and print all final list1 using Iterator.

import java.util.*;
public class JavaCollectionDemo {
  
public static void main(String[] args)
{

//String array
String[] things = {"eggs" , "hats" , 
"laser" , "pie"};

// Define an empty ArrayList 
List list1 =  new ArrayList();

// Add array items to list1
    for(String x: things)
    {
    list1.add(x);
    }


// Another String array 
String[] morethings = {"eggs" , "hats"};

// Define a second empty ArrayList 
List list2 =  new ArrayList();

// Add array items to list2
    for(String x: morethings)
    {
     list2.add(x);
    }

Print list1 elements 
     for (int i = 0; i// Call edit List Function to check
 whether list1 contains list2 items and if yes
 remove them from list1.
     editList(list1, list2);
     System.out.println();

// Display Final List1 elements
    for (int i = 0; i// Function to check whether list1 contains 
list2 items and if yes remove them from list1.
public static void editList(Collection l1 , Collection l2)
{
 Iterator iter = l1.iterator(); 
 while(iter.hasNext())
 {
   if(l2.contains(iter.next()))
   {
     iter.remove();
     
   }   
 }
}
}

Output

eggs   hats  laser   pie   
laser   pie

editList method : Firstly we created an iterator object iter. We iterated through list1 one by one and checked whether list2 contains the elements of list1 and if yes remove it.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>