Java Utililty Methods Iterator from

List of utility methods to do Iterator from

Description

The list of methods to do Iterator from are organized into topic(s).

Method

TgetIndexInIterator(Iterator iterator, int index)
get Index In Iterator
for (int i = 0; iterator.hasNext(); i++) {
    T current = iterator.next();
    if (i == index) {
        return current;
return null;
IterablegetIterable(final Iterator i)
Wraps iterator into iterable
return new Iterable<T>() {
    @Override
    public Iterator<T> iterator() {
        return i;
};
IteratorgetIterator(final int[] p, final Object[] data)
Answer iterator, which iterates over specified data array according to the specified permutation, that is data[p[0]],..,data[p[data.length-1]]
return new Iterator() {
    int pos = 0;
    public boolean hasNext() {
        return pos < data.length;
    public Object next() {
        return data[p[pos++]];
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove from immutable iterator!");
};
IteratorgetIterator(final Object o, final Class classElement)
Cast an object into a typed iterator.
final List<T> list = new ArrayList<>();
if (o != null && classElement != null && Iterator.class.isAssignableFrom(o.getClass())) {
    Iterator<?> mObj = (Iterator<?>) o;
    while (mObj.hasNext()) {
        Object obj = mObj.next();
        if (obj == null) {
            list.add(null);
        } else if (classElement.isAssignableFrom(obj.getClass())) {
...
IteratorgetIterator(List list)
get Iterator
Iterator i = list.iterator();
return i;
IteratorgetIterator(T[] array)
Construct an iterator from an array.
return Arrays.asList(array).iterator();
StringgetIteratorAsString(Iterator iter, final String separator)
Returns a string containing all entries found.
StringBuffer buf = new StringBuffer();
boolean isFirst = true;
while (iter.hasNext()) {
    if (isFirst)
        isFirst = false;
    else
        buf.append(separator);
    buf.append(iter.next());
...
StringgetIteratorOptionalValue(final Iterator stringIterator)
get Iterator Optional Value
return stringIterator.hasNext() ? stringIterator.next() : "";
TgetLast(Iterator iterator)
get Last
T last = null;
while (iterator.hasNext()) {
    last = iterator.next();
return last;
ListgetList(Iterator iterator)
Creates a list of all of the items in an iteration.
List<String> contents = new ArrayList<String>();
while (iterator.hasNext()) {
    contents.add(iterator.next());
return contents;