© Edward Sciore 2019
Edward ScioreJava Program Designhttps://doi.org/10.1007/978-1-4842-4143-1_6

6. Iterables and Iteration

Edward Sciore1 
Newton, MA, USA

This chapter addresses the following question: suppose that a variable holds a collection of objects; what code should you write to examine its elements? Technically speaking, this question is asking about iterables and iteration. A collection is an iterable, and the mechanism for examining its elements is iteration. The question can be rephrased as “How should I iterate through an iterable?”

Java supports multiple ways to iterate through an iterable, which can be divided into two categories: external iteration, in which you write a loop that examines each element of the iterable; and internal ...

Get Java Program Design: Principles, Polymorphism, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.