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

/**
 * Concatenates multiple iterables into a single iterable. The iterator exposed by the returned
 * iterable does not support {@link Iterator#remove()} even if the input iterables do.
 *
 * @throws NullPointerException if {@code iterables} or any of its elements are {@code null}
 *//*from   ww w.j a va2  s.c  o m*/
public static <T> Iterable<T> concat(List<Iterable<T>> iterables) {
    for (Iterable<T> iterable : iterables) {
        Objects.requireNonNull(iterable);
    }
    return new Iterable<T>() {
        @Override
        public Iterator<T> iterator() {
            if (iterables.size() == 0) {
                return Collections.emptyIterator();
            }
            return new Iterator<T>() {
                Iterator<Iterable<T>> cursor = iterables.iterator();
                Iterator<T> currentIterator = cursor.next().iterator();

                private void advance() {
                    while (!currentIterator.hasNext() && cursor.hasNext()) {
                        currentIterator = cursor.next().iterator();
                    }
                }

                @Override
                public boolean hasNext() {
                    advance();
                    return currentIterator.hasNext();
                }

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

    };
}

From source file:OneItemSet.java

public Iterator<E> iterator() {
    return new Iterator() {
        public boolean hasNext() {
            return hasNxt;
        }//ww  w . j  a  va2s  . c  o  m

        public E next() {
            if (hasNxt) {
                hasNxt = false;
                return item;
            }
            throw new NoSuchElementException();
        }

        public void remove() {
            item = null;
        }

        boolean hasNxt = true;
    };
}

From source file:com.github.jferard.pgloaderutils.loader.CSVRecordCleanerExample.java

@Override
public Iterable<String> cleanRecord(final CSVRecord record) {
    return new Iterable<String>() {
        @Override//from www  .j a v  a 2s . co  m
        public Iterator<String> iterator() {
            return new Iterator<String>() {
                private int i = 0;

                @Override
                public boolean hasNext() {
                    return this.i < record.size();
                }

                @Override
                public String next() {
                    String s = record.get(this.i);
                    if (this.i == 11 || this.i == 12 || this.i == 16) // numbers
                        s = s.replaceAll(",", "."); // from continental to US

                    this.i++;
                    return s;
                }

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

From source file:org.datalorax.populace.core.walk.inspector.ArrayInspector.java

@Override
public Iterator<RawElement> getElements(final Object instance, final Inspectors inspectors) {
    Validate.isTrue(instance.getClass().isArray(), "Expected array type, got: " + instance.getClass());
    final int length = Array.getLength(instance);

    return new Iterator<RawElement>() {
        int index = 0;

        @Override//from   ww  w  . j  av a  2 s . c  o m
        public boolean hasNext() {
            return index != length;
        }

        @Override
        public RawElement next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return new ArrayElement(index++, instance);
        }
    };
}

From source file:edu.byu.nlp.util.Iterables2.java

/**
 * @param labeled//from w ww.j  av a 2s  .com
 * @param unlabeled
 * @return
 */
public static <F, S> Iterable<Pair<F, S>> pairUp(final Iterable<? extends F> first,
        final Iterable<? extends S> second) {
    return new Iterable<Pair<F, S>>() {
        @Override
        public Iterator<Pair<F, S>> iterator() {
            return new Iterator<Pair<F, S>>() {

                private final Iterator<? extends F> firstIt = first.iterator();
                private final Iterator<? extends S> secondIt = second.iterator();

                @Override
                public boolean hasNext() {
                    return firstIt.hasNext() && secondIt.hasNext();
                }

                @Override
                public Pair<F, S> next() {
                    return Pair.<F, S>of(firstIt.next(), secondIt.next());
                }

                @Override
                public void remove() {
                    firstIt.remove();
                    secondIt.remove();
                }
            };
        }
    };
}

From source file:com.kylinolap.cube.invertedindex.InvertedIndexCLI.java

public static Iterable<Pair<ImmutableBytesWritable, ImmutableBytesWritable>> readSequenceKVs(
        Configuration hconf, String path) throws IOException {
    final Reader reader = new Reader(hconf, SequenceFile.Reader.file(new Path(path)));
    return new Iterable<Pair<ImmutableBytesWritable, ImmutableBytesWritable>>() {
        @Override/*from   w ww .j  a  va 2 s .  c o m*/
        public Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>> iterator() {
            return new Iterator<Pair<ImmutableBytesWritable, ImmutableBytesWritable>>() {
                ImmutableBytesWritable k = new ImmutableBytesWritable();
                ImmutableBytesWritable v = new ImmutableBytesWritable();
                Pair<ImmutableBytesWritable, ImmutableBytesWritable> pair = new Pair<ImmutableBytesWritable, ImmutableBytesWritable>(
                        k, v);

                @Override
                public boolean hasNext() {
                    boolean hasNext = false;
                    try {
                        hasNext = reader.next(k, v);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        if (hasNext == false) {
                            IOUtils.closeQuietly(reader);
                        }
                    }
                    return hasNext;
                }

                @Override
                public Pair<ImmutableBytesWritable, ImmutableBytesWritable> next() {
                    return pair;
                }

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

From source file:net.shipilev.fjptrace.util.PairedList.java

@Override
public Iterator<Pair> iterator() {
    return new Iterator<Pair>() {
        int i = 0;

        @Override// w w w. j  a v a 2 s.c o m
        public boolean hasNext() {
            return i < index;
        }

        @Override
        public Pair next() {
            Pair r = new Pair(k1[i], k2[i]);
            i++;
            return r;
        }

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

From source file:com.thoughtworks.studios.journey.utils.IterableUtils.java

public static <T> Iterable<T> uniqueBy(final Function<T, Object> function, final Iterable<T> iterable) {
    return new Iterable<T>() {
        @Override//from   www . j a va  2 s .  com
        public Iterator<T> iterator() {
            final Iterator<T> iterator = iterable.iterator();

            return new Iterator<T>() {
                Set<Object> keys = new HashSet<>();
                T nextItem;

                @Override
                public boolean hasNext() {
                    while (iterator.hasNext()) {
                        nextItem = iterator.next();
                        if (keys.add(function.apply(nextItem))) {
                            return true;
                        }
                    }

                    return false;
                }

                @Override
                public T next() {
                    if (nextItem == null && !hasNext()) {
                        throw new NoSuchElementException();
                    }

                    return nextItem;
                }

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

From source file:la.alsocan.jsonshapeshifter.bindings.ArrayConstantBinding.java

@Override
public Iterator<JsonNode> getValue(JsonNode payload, List<Integer> context) {
    return new Iterator<JsonNode>() {

        private int count = 0;

        @Override//from w ww .  jav a  2  s. c o  m
        public boolean hasNext() {
            return count < nbIterations;
        }

        @Override
        public JsonNode next() {
            count++;
            return NullNode.instance;
        }
    };
}

From source file:WeakHashSet.java

/**
 * Returns an iterator over the elements in this set.  The elements
 * are returned in no particular order./*www .  ja  va  2 s  . c  o m*/
 *
 * @return an Iterator over the elements in this set.
 */
public Iterator iterator() {
    // remove garbage collected elements
    processQueue();

    // get an iterator of the superclass WeakHashSet
    final Iterator i = super.iterator();

    return new Iterator() {
        public boolean hasNext() {
            return i.hasNext();
        }

        public Object next() {
            // unwrap the element
            return getReferenceObject((WeakReference) i.next());
        }

        public void remove() {
            // remove the element from the HashSet
            i.remove();
        }
    };
}