Java Iterator and Iterable

So this question came up in a job interview not too long ago:

Implement a method that takes as a parameter a varargs of Iterables and returns a unified Iterator (without merging the the passed in Iterables into some collection)

Creating a custom Iterator and/or Iterable is pretty basic stuff but admittedly I can’t remember the last time I’ve done it. While I know that implementing Iterator requires at least implementing the hasNext and next method and I kinda forgot what the purpose of Iterable basically just means that you are providing the iterator() method, duh. Anyway, I obviously need some refreshers but in the mean time here’s my future white boarding solution

 

Leave a Reply

Your email address will not be published. Required fields are marked *