Skip to content

Instantly share code, notes, and snippets.

@Phoenix124
Created August 20, 2018 07:46
Show Gist options
  • Save Phoenix124/a86db6f3fc8c13bab2db6b7360a616c1 to your computer and use it in GitHub Desktop.
Save Phoenix124/a86db6f3fc8c13bab2db6b7360a616c1 to your computer and use it in GitHub Desktop.
Iterator
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Converter {
Iterator<Integer> convert(Iterator<Iterator<Integer>> it) {
return new Iterator<Integer>() {
/**
* Current step of iterator
*/
Iterator<Integer> current = it.next();
/**
* Check next step of iterator
* @return true or false
*/
@Override
public boolean hasNext() {
boolean hasNext = false;
if (current.hasNext()) {
hasNext = true;
} else {
while (it.hasNext()) {
this.current = it.next();
if (current.hasNext()) {
hasNext = true;
break;
}
}
}
return hasNext;
}
/**
* Check next int in iterator and return it, else go to next iterator and return int of it
* @return next value of iterator
*/
@Override
public Integer next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
return current.next();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment