# Custom Iterator for a given List

• Implement a custom Iterator for a given ArrayList.
for example

``````for the given list implement an Iterator which will provide with elements which are greater than 5
List = {2,3,5,12,13,4}

so the custom iterator itr should work as:

itr.hasNext(): true;
itr.next(): 12;
itr.next(): 13;
itr.hasNext(): false;
``````

• @icemountain Thanks. This seem like a pretty basic coding exercise. `ArrayList<ArrayList<T>>` would be a little trickier to implement.

• hey @icemountain , can you share the company tag if it is possible?

``````                      public class CustomIterator implements Iterator<Integer> {

private int current;
private int num;
private Iterator<Integer> lsIterator;
public CustomIterator(List<Integer> ls, int num) {
lsIterator = ls.iterator();
this.num = num;
current = 0;
}
@Override
public boolean hasNext() {
while (lsIterator.hasNext()&& current <= num){
current = lsIterator.next();
}
return lsIterator.hasNext();
}

@Override
public Integer next() {
if (current > num) {
int tmp = current;
if (lsIterator.hasNext())
current = lsIterator.next();
return tmp;
}
else {
while (current <= num){
current = lsIterator.next();
}
return current;
}
}

}``````

• @elmirap Hi I found a bug in your code, it there is only one element that is greater than 5, your code won't work, say if we have {2,3,5,7} your hasNext() will return false but it should return true because 7 is greater than 5

• Here is a slight modification of above code

``````public class CustomerIterator implements Iterator<Integer>{

private Iterator<Intgeger> listI;
private int num;
private int current;

public CustomerIterator(List<Integer> list, int num){
listI = list.iteraot();
this.num = num;
this. current = Integer.MIN_VALUE;
}

public boolean hasNext(){
while(listI.hasNext() && cur <= num){
cur = listI.next();
}
return cur > num;
}

public int next(){
if(hasNext()){
int temp = current;
current = Integer.MIN_VALUE;
return temp;
}
throw Exception
}

}
``````

• Also ,we should consider case such as negative number. so the cur should be initialized as Integer.MIN_VALUE

• ZhackerZ, thank you very much for your modification.

• Is it correct to keep state in hasNext(). As one may invoke hasNext() multiple times without actually calling the next. @ZhackerZ - In this case cur gets updated. Also I get by cur you mean Current
Another version

``````   def  hasnext(self):
if(self.cursor > = len(self.list)):
return False
temp_cursor=self.cursor
item = list[temp_cursor]
while(temp_cursor < len(self.list) and item < self.limit):
temp_cursor+=1
return item < self.limit

def next(self):
if(self.cursor > len(self.list)):
raise Exception("No Item")

temp_cursor = self.cursor
item = list[temp_cursor]
while (temp_cursor < len(self.list) and item < self.limit):
temp_cursor += 1
self.cursor=temp_cursor
if(item < self.limit):
return item
else:
raise Exception ("No Item")
``````

• Solution Approach:

I thought about a general solution like an OOP exercise otherwise it would be a very basic implementation question.

So my design allows the usage of any Iterator as a data feed and it also allows the usage of any predicate that basically:
predicate Integer x -> boolean
Example:
predicate(Integer x) returns x > 5

This can be done easily in java 8 with the introduction of functional interfaces.

``````public interface Predicate<T> {
public boolean valid(T data);
}

/*-
* for the given list implement an Iterator which will provide with elements which are greater than 5
List = {2,3,5,12,13,4}

so the custom iterator itr should work as:

itr.hasNext(): true;
itr.next(): 12;
itr.next(): 13;
itr.hasNext(): false;
*/
public class CustomIterator<T> implements Iterator<T> {

private Iterator<T> itr;
private T next;
private Predicate<T> test;

public CustomIterator(Iterator<T> dataItr, Predicate<T> predicate) {
if (dataItr == null || predicate == null)
throw new IllegalArgumentException("No null params.");

this.itr = dataItr;
this.test = predicate;
this.next = null;
consume();
}

private void consume() {
while (itr.hasNext() && next == null) {
T cur = itr.next();
if (test.valid(cur))
next = cur;
}
}

@Override
public boolean hasNext() {
return next != null;
}

@Override
public T next() {
T value = next;
next = null;
consume();
return value;
}

// List = {2,3,5,12,13,4}
public static void main(String[] args) {
ArrayList<Integer> data = new ArrayList<Integer>();