Why java collection framework extends iterable interface even though it needs iterator object to go through the collection? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why java collection framework extends iterable interface even though it needs iterator object to go through the collection?

I'm very much confused about the relation between iterable and iterator interface.

14th Aug 2017, 5:08 AM
Backbencher nazrul
Backbencher nazrul - avatar
1 Answer
0
iterator can't be iterated using enhanced for loop, iterable can which is why collections extend them. iterator is actually an embedded​ classes inside many of the collection ADT. when used in enhanced for loop, iterator are automatically instantiated for you and call on to be iterated. it can't be done if it doesn't implement the iterable interface however
14th Aug 2017, 5:35 AM
Gao Xiangshuai
Gao Xiangshuai - avatar