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

private static <E> Collection<E> asCollection(final E[] elements) {
    return new AbstractCollection<E>() {

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

                // Object field
                private int index = 0;

                // Interface methods
                public boolean hasNext() {
                    return index < elements.length;
                }/* ww w . j a v  a  2s  .c  o  m*/

                public E next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    return elements[index++];
                }

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

            };
        }

        public int size() {
            return elements.length;
        }

    };
}

From source file:Main.java

public static <E> Iterable<E> reverse(final List<E> list) {
    return new Iterable<E>() {
        @Override/* w  ww  .j ava  2s.  co  m*/
        public Iterator<E> iterator() {
            final ListIterator<E> it = list.listIterator(list.size());

            return new Iterator<E>() {
                @Override
                public boolean hasNext() {
                    return it.hasPrevious();
                }

                @Override
                public E next() {
                    return it.previous();
                }

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

From source file:Main.java

/**
 * Repeats the same iterable over and over.
 * @param iterable to repeat//from ww  w .  ja v a 2 s .co  m
 * @param n specifies how often the iterable is repeated
 * @return n times repeated iterbale
 */
public static <T> Iterable<T> cycle(Iterable<T> iterable, int n) {
    return () -> new Iterator<T>() {
        private Iterator<T> iterator = iterable.iterator();
        private int count = 0;

        @Override
        public boolean hasNext() {
            if (!iterator.hasNext()) {
                iterator = iterable.iterator();
                count++;
            }
            return n < 0 || count < n;
        }

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

From source file:Main.java

public static <T> Iterable<T> replicate(final T value, final int count) {
    return new Iterable<T>() {
        @Override//from www .  j a v  a  2  s .  c om
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                int remaining = count;

                @Override
                public boolean hasNext() {
                    return remaining > 0;
                }

                @Override
                public T next() {
                    if (remaining-- <= 0)
                        throw new NoSuchElementException();
                    return value;
                }

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

From source file:Main.java

public static <T> Iterable<T> concat(Iterable<T> first, Iterable<T> second) {

    return new Iterable<T>() {

        final Iterator<T> it1 = first.iterator();
        final Iterator<T> it2 = second.iterator();

        @Override/* w  w w  .ja  va2 s.com*/
        public Iterator<T> iterator() {

            return new Iterator<T>() {

                Iterator<T> curr;

                @Override
                public boolean hasNext() {
                    if (it1.hasNext()) {
                        curr = it1;
                        return true;
                    } else if (it2.hasNext()) {
                        curr = it2;
                        return true;
                    }
                    return false;
                }

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

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> Iterable<T> concat(final Iterable<T>... collections) {
    return new Iterable<T>() {
        @Override/*from ww  w .ja va  2  s .c o  m*/
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                final Iterable<T>[] iterables = collections;
                int iteratorIdx = 0;
                Iterator<T> it = collections[0].iterator();

                private synchronized void advanceCursor() {
                    while (!it.hasNext() && iteratorIdx < iterables.length) {
                        iteratorIdx += 1;
                        if (iteratorIdx == iterables.length) {
                            it = new Iterator<T>() {
                                @Override
                                public boolean hasNext() {
                                    return false;
                                }

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

                                @Override
                                public void remove() {
                                    throw new RuntimeException();
                                }
                            };
                        } else {
                            it = iterables[iteratorIdx].iterator();
                        }
                    }
                }

                @Override
                public synchronized boolean hasNext() {
                    advanceCursor();
                    return iteratorIdx != iterables.length && it.hasNext();
                }

                @Override
                public synchronized T next() {
                    advanceCursor();
                    if (iteratorIdx == iterables.length)
                        throw new NoSuchElementException();
                    return it.next();
                }

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

From source file:Main.java

/**
 * Extracts child elements from node whith type <code>ELEMENT_NODE</code>.
 *
 * @param node root node of XML document for search.
 * @return iterator with proper node childs.
 *//*from www. j a v a 2  s  .com*/
public static Iterator<Element> getChildElements(final Node node) {
    //        node.normalize();
    return new Iterator<Element>() {
        private final NodeList nodes = node.getChildNodes();
        private int nextPos = 0;
        private Element nextElement = seekNext();

        public boolean hasNext() {
            return nextElement != null;
        }

        public Element next() {
            if (nextElement == null)
                throw new NoSuchElementException();
            final Element result = nextElement;
            nextElement = seekNext();
            return result;
        }

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

        private Element seekNext() {
            for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                final Node childNode = nodes.item(i);
                if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                    nextPos = i + 1;
                    return (Element) childNode;
                }
            }
            return null;
        }
    };
}

From source file:Permutator.java

public static <T> Iterable<List<T>> permutations(final List<T> list) {
    return new Iterable<List<T>>() {
        public Iterator<List<T>> iterator() {
            return new Iterator<List<T>>() {
                private int current = 0;
                private final long length = factorial(list.size());

                public List<T> next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }//from  ww  w.  j  av  a2s  .c o m
                    List<T> permutation = new ArrayList<T>(list);
                    int k = current;
                    for (int j = 2; j <= list.size(); j++) {
                        k /= j - 1;
                        Collections.swap(permutation, (k % j), j - 1);
                    }
                    current++;
                    return permutation;
                }

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

                public boolean hasNext() {
                    return current < length;
                }
            };
        }
    };
}

From source file:Main.java

/**
 * Split an iterator into chunks -- this is useful for, e.g., splitting work among threads when it
 * input iterator has very short jobs./*from w w w . j a v  a2 s  . c om*/
 *
 * @param input The input iterator to chunk.
 * @param chunkSize The number of elements to include in each chunk (except the last one).
 * @param <E> The element type of the input iterator.
 * @return An iterator corresponding to the input, but chunked into groups.
 */
public static <E> Iterator<Collection<E>> chunk(final Iterator<E> input, final int chunkSize) {
    return new Iterator<Collection<E>>() {
        @Override
        public synchronized boolean hasNext() {
            return input.hasNext();
        }

        @Override
        public synchronized Collection<E> next() {
            List<E> rtn = new ArrayList<>();
            for (int i = 0; i < chunkSize; ++i) {
                if (input.hasNext()) {
                    rtn.add(input.next());
                }
            }
            return rtn;
        }

        @Override
        public void remove() {
            throw new RuntimeException("Remove is not implemented");
        }
    };
}

From source file:Main.java

public static Iterable<Node> iterate(final NodeList nodeList) {
    return new Iterable<Node>() {
        @Override/*from ww w . j  ava 2s  .c om*/
        public Iterator<Node> iterator() {

            return new Iterator<Node>() {

                int index = 0;

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

                @Override
                public Node next() {
                    return nodeList.item(index++);
                }

                @Override
                public boolean hasNext() {
                    return index < nodeList.getLength();
                }
            };
        }
    };
}