How to make custom collections iterable?

java-iterable-pencilThe benefits of using an iterator is to allow the the client to iterate over the collection of objects, without exposing implementation details. This gives you the benefit to change what collection the Iterator iterates over and how each element is served, without making any change the client’s code. To iterate a custom collection, class need to implements Iterable interface.

A class that implements the Iterable can be used with the new for-loop. Here is such an example:

The Iterable interface has only one method:

Now create a java class to add into that ArrayList

Now, create a class PencilArrayList which implements Iterable interface.

About the Author

has written 68 posts.

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect. Google Wanna Hire me ?

Copyright © 2017 http://www.techzoo.org. All rights reserved.
Proudly powered by WordPress. Developed by 7Tech Solutions