List of usage examples for java.lang Iterable iterator
Iterator<T> iterator();
From source file:com.github.steveash.jg2p.util.Zipper.java
public static <A, B> List<Pair<A, B>> up(Iterable<A> a, Iterable<B> b) { ArrayList<Pair<A, B>> result = Lists.newArrayList(); Iterator<A> iterA = a.iterator(); Iterator<B> iterB = b.iterator(); while (iterA.hasNext()) { Preconditions.checkArgument(iterB.hasNext(), "B is shorter than A, must be same size"); A aa = iterA.next();//from w w w.j a va 2 s . c o m B bb = iterB.next(); result.add(Pair.of(aa, bb)); } Preconditions.checkArgument(!iterB.hasNext(), "A is shorter than B, must be same size"); return result; }
From source file:Main.java
/** * Pick all elements from the iterable {@literal items} and convert their string representation into a * {@literal separator}-separated single string. * * @param items The source item collection * @param separator The separator string between items * <p/>//from w w w .java2s . c o m * @return A String containing the concatenated string representations of the elements in {@literal items}. */ public static String join(final Iterable<?> items, final String separator) { final StringBuilder sequence = new StringBuilder(); final Iterator<?> iter = items.iterator(); if (iter.hasNext()) { final String first = iter.next().toString(); sequence.append(first); while (iter.hasNext()) { sequence.append(separator); final String next = iter.next().toString(); sequence.append(next); } } return sequence.toString(); }
From source file:net.daboross.bukkitdev.skywars.gist.GistReport.java
public static String joinText(Iterable<String> iterable) { Iterator<String> iterator = iterable.iterator(); if (!iterator.hasNext()) { return ""; }/*from ww w . ja v a2 s. co m*/ StringBuilder builder = new StringBuilder(iterator.next()); while (iterator.hasNext()) { builder.append("\n").append(iterator.next()); } return builder.toString(); }
From source file:Main.java
public static <E> ArrayList<E> newArrayList(Iterable<E> iterable) { if (iterable == null) { throw new NullPointerException(); }/*from w w w .j av a2 s.c o m*/ ArrayList<E> list = new ArrayList<E>(); for (Iterator<E> i = iterable.iterator(); i.hasNext();) { list.add(i.next()); } return list; }
From source file:com.github.steveash.jg2p.util.Zipper.java
public static <A, B> List<Pair<A, B>> replaceRight(List<Pair<A, B>> original, Iterable<B> newRight) { ArrayList<Pair<A, B>> result = Lists.newArrayListWithCapacity(original.size()); Iterator<B> iter = newRight.iterator(); for (Pair<A, B> pair : original) { Preconditions.checkArgument(iter.hasNext(), "newRight is smaller than original"); result.add(Pair.of(pair.getLeft(), iter.next())); }//from ww w. j av a 2 s.com Preconditions.checkArgument(!iter.hasNext(), "newRight is bigger than original"); return result; }
From source file:net.dontdrinkandroot.utils.collections.CollectionUtils.java
public static <T> T first(Iterable<T> iterable) { return iterable.iterator().next(); }
From source file:Main.java
/** * writes the objects in iterable to the returned String, separating them with the given separator. * String.valueOf() is used to write each element of the collection. * @param iterable/* w w w. ja va 2 s. c o m*/ * @param sSeparator if non-empty, is written between each two elements (that is, not before the first one, and not after the last one) * @param sPrefix if non-empty, is prepended before each element. * @param sSuffix if non-empty, is appended after each element. * @return a String containing the String representations of the given Collection's elements. * @precondition iterable != null * @postcondition result != null */ public static String getSeparatedList(Iterable<?> iterable, String sSeparator, String sPrefix, String sSuffix) { final StringBuilder sb = new StringBuilder(); for (Iterator<?> iter = iterable.iterator(); iter.hasNext();) { // prefix: if (sPrefix != null) { sb.append(sPrefix); } // the element itself: sb.append(iter.next()); // suffix: if (sSuffix != null) { sb.append(sSuffix); } // separator: if (sSeparator != null && iter.hasNext()) { sb.append(sSeparator); } } return sb.toString(); }
From source file:fr.lirmm.graphik.util.MathUtils.java
/** * Comput the cartesian product of the specified set with itself. * input: { A, B, C }// w w w .java2 s . co m * output : { (A,A), (A,B), (B,B) } * @param set * @return */ public static <T> Iterable<Pair<T, T>> selfCartesianProduct(Iterable<T> set) { Collection<Pair<T, T>> pairs = new LinkedList<Pair<T, T>>(); Iterator<T> it = set.iterator(); while (it.hasNext()) { T a = it.next(); for (T b : set) { pairs.add(new ImmutablePair<T, T>(a, b)); } if (it.hasNext()) { // FIXfor singleton implementation it.remove(); } } return pairs; }
From source file:com.insightml.utils.ui.UiUtils.java
private static String toString(final Iterable<?> list) { if (!list.iterator().hasNext()) { return "Empty List"; }/*from w w w . ja va 2s . com*/ final StringBuilder builder = new StringBuilder(); int maxFirst = 30; for (final Object entry : list) { if (entry instanceof Triple) { maxFirst = Math.max(maxFirst, ((Triple<?, ?, ?>) entry).getFirst().toString().length()); } } for (final Object entry : list) { if (entry instanceof Triple) { builder.append(fill(((Triple<?, ?, ?>) entry).getFirst(), maxFirst + 10)); builder.append(fill(((Triple<?, ?, ?>) entry).getSecond(), 30)); builder.append(((Triple<?, ?, ?>) entry).getThird()); } else { builder.append(format(entry)); } builder.append('\n'); } return builder.toString(); }
From source file:Main.java
/** * Get the first element of the passed iterable. * * @param <ELEMENTTYPE>/*w ww . j av a 2 s. c o m*/ * Iterable element type * @param aIterable * The iterable. May be <code>null</code>. * @return <code>null</code> if the iterable is <code>null</code> or empty, * the first element otherwise. */ @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getFirstElement(@Nullable final Iterable<ELEMENTTYPE> aIterable) { if (aIterable == null) return null; final Iterator<ELEMENTTYPE> it = aIterable.iterator(); return it.hasNext() ? it.next() : null; }