dimanche 4 octobre 2020

Implementing iterator design pattern

I have a class Example witch private field Hashmap<Integer, CustomObject>. My goal is to access all instances of CustomObject in this class using the Iterable interface. First, I declare Example to implement Iterable<CustomObject>. Then I call iterator() method. However, I don't know if I should specify hasNext() etc, what to put in main code? Here is what I have so far:

public class Example implements Iterable<Songs>{
  private HashMap <Integer, CustomObject>;

  @Override
  public Iterator<CustomObject> iterator() {
    for (CustomObject customObject: this){
      System.out.println(customObject);
    }

  public static void main(String[] args) {
    Example.iterator();
  }

Aucun commentaire:

Enregistrer un commentaire