🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 5 Min Lesezeit
0

Iterator Pattern

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




What is Iterator Pattern?




Iterator pattern is a behavioral pattern that provides a way to access the elements of an aggregate (collection) object sequentially without exposing its underlying representation.







When to use it?




  • Use Iterator pattern when you don't want to expose the data structure of your collection.

  • User Iterator pattern when you want one common interface to traverse different data structures instead of having many similar traversal codes for each data structure in your app.






Problem



Company "Alpha Inc." is going to merge with "Beta Inc.". We are in HR department and have to merge their employees data together.

EmployeeListA holds Alpha employees' data, and EmployeeListB has Beta employees' data. Because EmployeeListA uses Array data structure and EmployeeListB uses ArrayList, we write two traversal codes.





We need Iterator interface to decouple iteration logic from HRDepartment.




CODE
public interface Iterator {
boolean hasNext();
String next();
}






hasNext() returns boolean value indicating whether a collection has next element or not. next() returns next element in a collection. (you will see actual implementation later)



In EmployeeListA/EmployeeListA, We removed getEmployees() method because it exposes our aggregate's data structure. And importantly, we wrote new method createIterator() which creates an corresponding concrete Iterator, for example, EmployeeListA.createIterator() instantiates EmployeeListAIterator.






Introduce common interface for aggregates



Okay, we now encapsulated Iteration logic, our aggregates do not expose their underlying representation, removed duplicate traversal codes. But still one problem remains, HRDepartment depends on concrete aggregates. Let's introduce a common interface for concrete aggregates.








Implementation in Java



I'll omit EmployeeListBIterator and EmployeeListB's implementation as they are similar to those of EmployeeListAIterator and EmployeeListA. If you're not sure how to implement them, you can check in my GitHub repo (link to my repo is at the end of this blog).




CODE
public interface Iterator {
boolean hasNext();
String next();
}









CODE
public class EmployeeListAIterator implements Iterator {

private String[] employees;
private int index = 0;

public EmployeeListAIterator(String[] employees) {
this.employees = employees;
}

@Override
public boolean hasNext() {
if (index >= employees.length || employees[index] == null) {
return false;
}
return true;
}

@Override
public String next() {
String employee = employees[index];
index++;
return employee;
}
}









CODE
public interface EmployeeList {
Iterator createIterator();
String getCompanyName();
}









CODE
public class EmployeeListA implements EmployeeList{

private String companyName;
private String[] employees;
private int SIZE = 5;
private int index = 0;

public EmployeeListA(String companyName) {
this.companyName = companyName;
employees = new String[SIZE];
addEmployee("Alice");
addEmployee("Alisha");
addEmployee("Alex");
}

public void addEmployee(String name) {
employees[index] = name;
index++;
}

public Iterator createIterator() {
return new EmployeeListAIterator(employees);
}

public String getCompanyName() {
return companyName;
}
}









CODE
public class HRDepartment {

EmployeeList listA;
EmployeeList listB;

public HRDepartment(EmployeeList listA, EmployeeList listB) {
this.listA = listA;
this.listB = listB;
}

public void printEmployee() {
Iterator listAIterator = listA.createIterator();
Iterator listBIterator = listB.createIterator();

System.out.println("--- Employees from " + listA.getCompanyName() + " ---");
printEmployee(listAIterator);
System.out.println("--- Employees from " + listB.getCompanyName() + " ---");
printEmployee(listBIterator);
}

private void printEmployee(Iterator iterator) {
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}






Output:




CODE
--- Employees from Alpha Inc ---
Alice
Alisha
Alex
--- Employees from Beta Inc ---
Bob
Bella
Benjamin









Pitfalls




  • Iterator itself imply no ordering in which elements are visited during iteration. The order of iteration depends on the underlying data structure being traversed, not on the Iterator.

  • Iterator does not support accessing elements by index.






Iterator & Iterable interface in Java



Before the end of this blog, let's check how we can use java.util.Iterator.



Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Iterator Pattern

Thematisch verwandte Begriffe: Iterator, Pattern · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...