Java Iterator — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 44 / 50 · தமிழ்

Java Iterator

Visit elements with an explicit cursor.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Iterator.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

An Iterator separates traversal from the collection implementation. hasNext checks whether another element exists; next consumes it. When supported, iterator.remove removes the most recently returned element in a controlled way.

02 / Step by step

விரிவான விளக்கங்கள்

Java Iterator என்னன்னு தெரியுமா?

Iterator என்பது collection-ஐ loop பண்றதுக்கு use ஆகும் — while iterating elements remove பண்றது possible.

iterator() method call பண்ணி Iterator get பண்ணலாம்:

Iterator Methods

  • hasNext() — அடுத்த element இருக்கான்னு check பண்றது
  • next() — அடுத்த element return பண்றது
  • remove() — current element remove பண்றது
Java reference fragmentJAVA
ArrayList<String> cars = new ArrayList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");

Iterator<String> it = cars.iterator();
while (it.hasNext()) {
    System.out.println(it.next());
}
// Volvo, BMW, Ford

// Safe removal
Iterator<String> it2 = cars.iterator();
while (it2.hasNext()) {
    if (it2.next().equals("BMW")) it2.remove();
}
System.out.println(cars);  // [Volvo, Ford]

Iteration-ல் `ConcurrentModificationException` எப்போது வரும்?

  1. Large collections iterate செய்யும்போது
  2. Iterator-ஐ use செய்து iterate செய்யும்போது collection-ஐ modify செய்தால்
  3. Empty collection iterate செய்தால்
  4. Nested loops பயன்படுத்தினால்
Reveal answer / விடை

Iterator-ஐ use செய்து iterate செய்யும்போது collection-ஐ modify செய்தால்

for-each loop-ல் iterate செய்யும்போது collection modify செய்தால் `ConcurrentModificationException` வரும். Remove செய்ய Iterator-ன் `iterator.remove()` method பயன்படுத்தவும்.

03 / Read and predict

முழுமையான உதாரணம்

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            if (iterator.next() % 2 == 0) iterator.remove();
        }
        System.out.println(numbers);
    }
}
Predict first. Then reveal the expected output / வெளியீடு
[1, 3]

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Change the condition to remove odd values instead.

Reveal the answer and reasoning / விடை

The remaining list is [2, 4].

தவிர்க்க வேண்டிய தவறு: Do not structurally modify the list directly while traversing it with its iterator. Fail-fast checks are diagnostic, not a concurrency guarantee.

பாடத்தின் சுருக்கம்

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.