List of usage examples for java.util Iterator Iterator
Iterator
From source file:Main.java
public static Iterable<String> iterate(Matcher matcher) { return new Iterable<String>() { @Override// www. j a v a2s . c o m public Iterator<String> iterator() { return new Iterator<String>() { @Override public boolean hasNext() { return matcher.find(); } @Override public String next() { return matcher.group(); } }; } @Override public void forEach(Consumer<? super String> action) { while (matcher.find()) { action.accept(matcher.group()); } } }; }
From source file:Main.java
static <T> Iterator<T> cycle(final Iterable<T> iterable) { return new Iterator<T>() { Iterator<T> iterator = Collections.<T>emptySet().iterator(); @Override// w w w .j a v a 2 s . co m public boolean hasNext() { return true; } @Override public T next() { if (!iterator.hasNext()) { iterator = iterable.iterator(); } return iterator.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:Main.java
/** * Converts a w3c DOM nodelist into an iterator. * @param nl the node list// w w w.j a v a2 s. c om * @return the iterator wrapping the list of DOM nodes */ public static Iterator nodeList2Iterator(final NodeList nl) { final int[] i = new int[] { 0 }; return new Iterator() { public boolean hasNext() { return i[0] < nl.getLength(); } public Object next() { return nl.item(i[0]++); } public void remove() { throw new UnsupportedOperationException(); } }; }
From source file:Main.java
public static <T1, T2> Collection<T2> lazyMap(final Collection<T1> lst, final Function<T1, T2> mapper) { return new AbstractCollection<T2>() { @Override//from w w w. jav a 2s .c om public Iterator<T2> iterator() { return new Iterator<T2>() { Iterator<T1> impl = lst.iterator(); @Override public boolean hasNext() { return impl.hasNext(); } @Override public T2 next() { return mapper.apply(impl.next()); } @Override public void remove() { impl.remove(); } }; } @Override public int size() { return lst.size(); } }; }
From source file:Main.java
public static <T> Iterable<T> reverse(final List<T> list) { return new Iterable<T>() { public Iterator<T> iterator() { final ListIterator<T> listIterator = list.listIterator(list.size()); return new Iterator<T>() { public boolean hasNext() { return listIterator.hasPrevious(); }/*from w ww . j ava 2 s .c om*/ public T next() { return listIterator.previous(); } public void remove() { listIterator.remove(); } }; } }; }
From source file:Main.java
/** * @param value value to return on each iteration * @param n specifies how often the value is repeated * @return iterable returning n times the passed in value *//*from ww w . j ava2 s. c o m*/ @SuppressWarnings("unchecked") public static <T> Iterable<T> repeat(T value, int n) { if (value == null) { return NULL_REPEATABLE; } return () -> new Iterator() { private int count = 0; @Override public boolean hasNext() { return n < 0 || count < n; } @Override public Object next() { count++; return value; } }; }
From source file:Main.java
public static <T> Iterable<T> concat(final List<Iterable<? extends T>> iterables) { return new Iterable<T>() { public Iterator<T> iterator() { return new Iterator<T>() { Iterator<? extends T> iter; int pos = 0; public boolean hasNext() { while (pos < iterables.size()) { if (iter == null) iter = iterables.get(pos).iterator(); if (iter.hasNext()) return true; iter = null;/* w w w . java 2s .c o m*/ pos++; } return false; } public T next() { while (pos < iterables.size()) { if (iter == null) iter = iterables.get(pos).iterator(); if (iter.hasNext()) return iter.next(); iter = null; pos++; } throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException("remove"); } }; } }; }
From source file:Main.java
public static <T> Iterator<T> singletonIterator(final T item) { return new Iterator<T>() { private boolean gotItem = false; public boolean hasNext() { return !this.gotItem; }// w w w .j a v a2s . co m public T next() { if (this.gotItem) { throw new NoSuchElementException(); } this.gotItem = true; return item; } public void remove() { if (!this.gotItem) { this.gotItem = true; } else { throw new NoSuchElementException(); } } }; }
From source file:Main.java
public static <T> Iterator<T> iteratorUnion(final Iterator<T>[] iterators) { return new Iterator<T>() { private int iteratorIndex = 0; private Iterator<T> current = iterators.length > 0 ? iterators[0] : null; public boolean hasNext() { for (;;) { if (current == null) { return false; }//from ww w. j ava 2s .c o m if (current.hasNext()) { return true; } iteratorIndex++; current = iteratorIndex >= iterators.length ? null : iterators[iteratorIndex]; } } public T next() { for (;;) { if (this.current == null) { throw new NoSuchElementException(); } try { return this.current.next(); } catch (final NoSuchElementException nse) { this.iteratorIndex++; this.current = this.iteratorIndex >= iterators.length ? null : iterators[this.iteratorIndex]; } } } public void remove() { if (this.current == null) { throw new NoSuchElementException(); } this.current.remove(); } }; }
From source file:com.px100systems.util.SpringELCtx.java
/** * First element of anything iterable including arrays * @param list the list//from w w w. j a v a 2 s .c o m * @return the first element or null */ @SuppressWarnings("unchecked") public static Object firstElement(Object list) { if (list == null) return null; if (list instanceof Iterator) { Iterator<Object> l = (Iterator<Object>) list; return l.hasNext() ? l.next() : null; } if (list instanceof Collection) { Collection<Object> l = (Collection<Object>) list; if (l.isEmpty()) return null; return firstElement(l.iterator().next()); } if (list instanceof Object[]) { Object[] l = (Object[]) list; if (l.length == 0) return null; return firstElement(l[0]); } return list; }