I am a beginner and I cannot understand the real effect of the Iterable interface.
I am a beginner and I cannot understand the real effect of the Iterable
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Besides what Jeremy said, its main benefit is that it has its own bit of syntactic sugar: the enhanced for-loop. If you have, say, an
Iterable<String>, you can do:Nice and easy, isn’t it? All the dirty work of creating the
Iterator<String>, checking if ithasNext(), and callingstr = getNext()is handled behind the scenes by the compiler.And since most collections either implement
Iterableor have a view that returns one (such asMap‘skeySet()orvalues()), this makes working with collections much easier.The
IterableJavadoc gives a full list of classes that implementIterable.