Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ClubbedTroll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ClubbedTroll {
private final String name;
private final int level;

public ClubbedTroll(String name, int level) {
this.name = name;
this.level = level;
}

// Other methods...
}
6 changes: 3 additions & 3 deletions iterator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Sequence diagram
The main class in our Java Iterator Design Pattern example is the `TreasureChest` that contains items. This demonstrates how to implement and use iterators for efficient collection traversal in Java.

```java
public class TreasureChest {
public class TreasureChest implements Iterable<Item> { //marking Iterable or overriding to get Iterator<Item>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declares the class as Iterable but the example snippet does not include the required iterator() method with no parameters. Iterable contract requires public Iterator iterator(). The current snippet will not compile as-is.


private final List<Item> items;

Expand All @@ -61,8 +61,8 @@ public class TreasureChest {
new Item(ItemType.WEAPON, "Steel halberd"),
new Item(ItemType.WEAPON, "Dagger of poison"));
}

public Iterator<Item> iterator(ItemType itemType) {
@Override //method which have to be overriden if this implements Iterable interface in java
public Iterator<Item> iterator(ItemType itemType) {
return new TreasureChestItemIterator(this, itemType);
}
Comment on lines +65 to 67
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method signature does not override the Iterable.iterator() contract. Using @OverRide on a method with a parameter will fail to compile. If you want a type-filtered iterator, provide a separate method with a different name (e.g., iteratorByType) and keep a parameterless iterator() for the Iterable interface.


Expand Down
Loading