Example usage for java.util Iterator Iterator

List of usage examples for java.util Iterator Iterator

Introduction

In this page you can find the example usage for java.util Iterator Iterator.

Prototype

Iterator

Source Link

Usage

From source file:Main.java

public static <T> Iterator<T> emptyIterator() {
    return new Iterator<T>() {
        @Override/* w  ww  .  j  a  v  a 2s  .c o  m*/
        public boolean hasNext() {
            return false;
        }

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

        @Override
        public void remove() {
        }
    };
}

From source file:Main.java

public static <T> Iterator<T> readOnlyIterator(final Iterator<T> iterator) {
    return new Iterator<T>() {
        @Override//  www  . j a  va2s  .  c  om
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public T next() {
            return iterator.next();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("read only iterator");
        }
    };
}

From source file:EnumerationIterator1.java

public static Iterator iterator(final Enumeration e) {
    return new Iterator() {
        public boolean hasNext() {
            return e.hasMoreElements();
        }//  www .j a  v  a 2 s .co  m

        public Object next() {
            return e.nextElement();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:Main.java

public static <E> Iterator<E> asIterator(final Enumeration<E> e) {
    return new Iterator<E>() {
        @Override//  w ww.j ava 2 s.  com
        public boolean hasNext() {
            return e.hasMoreElements();
        }

        @Override
        public E next() {
            return e.nextElement();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:Main.java

public static <ELEMENT_TYPE> Iterator<ELEMENT_TYPE> unmodifiableEmptyIterator() {
    return new Iterator<ELEMENT_TYPE>() {

        @Override/*from   ww  w. j  a v a  2 s .c  om*/
        public boolean hasNext() {
            return false;
        }

        @Override
        public ELEMENT_TYPE next() {
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("This collection is imutable and empty");
        }

    };
}

From source file:Main.java

public static <T> Iterator<T> iterator(Enumeration<T> enumeration) {
    return new Iterator<T>() {

        @Override//  w w w  .  ja v a2s .  co m
        public boolean hasNext() {
            return enumeration.hasMoreElements();
        }

        @Override
        public T next() {
            return enumeration.nextElement();
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> makeEmptyIterable() {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                public boolean hasNext() {
                    return false;
                }//from  w w w  .j  a  va2s  . c o m

                public T next() {
                    return null;
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove not allowed");
                }
            };
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> cycle(final Iterable<T> it) {
    return new Iterable<T>() {

        public Iterator<T> iterator() {
            return new Iterator<T>() {

                private Iterator<T> nextCycle = it.iterator();

                public boolean hasNext() {
                    return true; // forever cycles
                }/* w ww  .ja v  a  2 s  .  c  o  m*/

                public T next() {
                    if (!nextCycle.hasNext()) {
                        nextCycle = it.iterator(); // create the next cycle
                    }
                    return nextCycle.next();
                }
            };
        }
    };
}

From source file:Main.java

/**
 * Generate an iterator for an array//from w w w. j a v a 2  s  .  co  m
 * 
 * @param array
 * @return
 */
public static <T> Iterator<T> getIterator(final T[] array) {
    return new Iterator<T>() {
        private int i = 0;

        public boolean hasNext() {
            return i < array.length;
        }

        public T next() {
            return array[i++];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:Main.java

public static <T> Iterable<T> asIterable(final T[] a) {
    return new Iterable<T>() {
        @Override//from   ww w  .j a va  2  s  .c o m
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int index = 0;

                @Override
                public boolean hasNext() {
                    return index < a.length;
                }

                @Override
                public T next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return a[index++];
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
        }
    };
}