#Week 51 — What is `Iterable` and how does it relate to for-each/enhanced `for` loops?

9 messages · Page 1 of 1 (latest)

dull boneBOT
#
Question of the Week #51

What is Iterable and how does it relate to for-each/enhanced for loops?

full wrenBOT
#

Iterable is an interface for objects that can be iterated over. It contains a single method named iterator which should return an Iterator object. This object allows to navigate through the Iterable and access its elements. It has a hasNext method which checks whether further elements are available and a next method which gets the current element and moves to the next.

As an example, the following Range record implements Iterable to iterate over Integers starting from a specific element until a specific element with a given step size.

public record Range(int start, int end, int stepSize) implements Iterable<Integer>{
  public Range{
    //vaidation in constructor
    if(end <= start) {
      throw new IllegalArgumentException("end must be after start");
    }

    if(stepSize <= 0) {
      throw new IllegalArgumentException("stepSize must be strictly positive");
    }
  }
  @Override
  public Iterator<Integer> iterator(){
    return new Iterator<>(){
      private int current = start;
      @Override
      public boolean hasNext(){
        //checks whether an element is available
        return current < end;
      }
      @Override
      public Integer next(){
        if(!hasNext()){
          //throw an exception if no element is available
          throw new NoSuchElementException("next() called but no next element available");
        }
        //return the current element and move to the next
        Integer ret = current;
        current += stepSize;
        return ret;
      }
    };
  }
}
#

Once we have an Iterable object, an enhanced for loop can be used to iterate over it.

for(Integer i:new Range(0,10,2)){
    System.out.println(i);
}

This is equivalent to the following code:

Range range = new Range(0,10,2);
for(Iterator<Integer> it = range.iterator(); it.hasNext();){
  Integer i = it.next();
  System.out.println(i);
}
📖 Sample answer from dan1st
full wrenBOT
#

It is an interface, that allows you to call the forEach method on the collection of data you are making. Example would be an Inventory class that takes a collection of Products.

Submission from smokeintitan
#

it is an interface which Collection also implement and

#
for(T t : this) {
  // iterables 

}

#

this how forEach is implemented

Submission from kokoko2023
#

Doesn't it allow objects to be the target of enhanced for-each/enhanced loops.

Submission from PNJ3.0#1519
#

Iterable is the posibility of iterating variable like integer ,double which well achieved during loops like for each and enhanced for loops
Here is code snippet:
int[] goals={2,4,5,8,1,0};
for(int position:goals){
System.out.println(position);
}
//the end.

Submission from trailblazer_g