Problem : Given an iterator of integers, can you give me back another iterator which iterates over the given iterator of integers such that the new iterator gives only even integers.
There a couple of different ways to solve this problem.
* The first option is to create a new list of integers and add only the even integers from the given iterator and then return an iterator by calling list.iterator(). This has a time complexity of O (n) but also has a space complexity of O (n) in the worst case, which is not desirable.
Here is the code for it -
* The second solution which is more efficient is to implement a new EvenIntegersIterator which takes in the given iterator as a parameter in the constructor and delegates the hasNext and next calls to the original iterator with logic to skip odd integers. This solution has a time complexity of O (n) in the worst case, but space complexity of O(1) as no extra memory is needed. Here is the code -
There a couple of different ways to solve this problem.
* The first option is to create a new list of integers and add only the even integers from the given iterator and then return an iterator by calling list.iterator(). This has a time complexity of O (n) but also has a space complexity of O (n) in the worst case, which is not desirable.
Here is the code for it -
public static Iterator<Integer> giveEvenIntegersIterator(Iterator<Integer> iterator)
{
List<Integer> evenIntegers = new ArrayList<Integer>();
if (iterator != null)
{
while(iterator.hasNext())
{
Integer number = iterator.next();
if (number != null && number % 2 == 0)
{
evenIntegers.add(number);
}
}
}
return evenIntegers.iterator();
}
* The second solution which is more efficient is to implement a new EvenIntegersIterator which takes in the given iterator as a parameter in the constructor and delegates the hasNext and next calls to the original iterator with logic to skip odd integers. This solution has a time complexity of O (n) in the worst case, but space complexity of O(1) as no extra memory is needed. Here is the code -
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.ArrayList;
/**
* This iterator implementation takes in an iterator of integers.
*
* This implementation will iterate over even integers only.
*/
public class EvenNumbersIterator implements Iterator
{
private Iterator<Integer> iterator;
private boolean hasNextCalled;
private Integer next;
private boolean hasNextValue;
/**
* Constructor which takes in an Iterator of Integers.
* @param iterator - Iterator of integers.
* @throws RuntimeException - Throws runtime exception if the iterator being passed is null.
*/
public EvenNumbersIterator(Iterator<Integer> iterator)
{
if (iterator == null)
{
throw new RuntimeException("Iterator cannot be null.");
}
this.iterator = iterator;
}
/**
* This returns if the iterator has the next element.
* @return boolean
*/
public boolean hasNext()
{
boolean hasNext = false;
if (hasNextCalled)
{
hasNext = hasNextValue;
}
else
{
hasNextCalled = true;
while (hasNext = iterator.hasNext())
{
Integer number = iterator.next();
if (number != null && number % 2 == 0)
{
next = number;
break;
}
}
hasNextValue = hasNext;
}
return hasNext;
}
/**
* Returns the next element of the iterator. Throws NoSuchElementException if there is no more elements.
* @return Object
*/
public Object next()
{
Integer returnValue = null;
if (hasNextCalled)
{
hasNextCalled = false;
returnValue = next;
if (returnValue == null)
{
throw new NoSuchElementException();
}
}
else
{
while(iterator.hasNext())
{
returnValue = iterator.next();
if (returnValue != null && returnValue % 2 == 0)
{
break;
}
}
if (returnValue == null)
{
throw new NoSuchElementException();
}
}
return returnValue;
}
public void remove()
{
}
}
Comments