List of utility methods to do Iterable Item
boolean | isPrefix(Iterable suspectedPrefix, Iterable container) there's no point in being "type safe" in this method, as the returned value is not typed, and we would like to support any type of iterable comparison. final Iterator spi = suspectedPrefix.iterator(); final Iterator ci = container.iterator(); while (true) { if (!spi.hasNext()) { return true; if (!ci.hasNext()) { return false; ... |
boolean | iterableContains(Iterable extends E> i, E match) iterable Contains for (E e : i) { if (Objects.equals(e, match)) { return true; return false; |
Collection | maxes(Iterable maxes LinkedList<T> maxes = new LinkedList<T>(); for (T element : elements) { if (maxes.size() == 0) { maxes.add(element); } else { int comparison = maxes.getFirst().compareTo(element); if (comparison == 0) { maxes.add(element); ... |
Iterable | prefix(final Iterable prefix return new Iterable<String>() { public Iterator<String> iterator() { return prefix(lines.iterator(), prefix); }; |
String | print(Iterable Prints all items in a collection to a string, separated by a custom separator. StringBuilder builder = new StringBuilder(); Iterator<T> it = coll.iterator(); while (it.hasNext()) { T item = it.next(); builder.append(item.toString()); if (it.hasNext()) builder.append(sep); return builder.toString(); |
Iterator | reverse(Iterable reverse LinkedList<T> reversed = new LinkedList<>(); for (T value : values) { reversed.add(value); return reversed.descendingIterator(); |
T | single(Iterable extends T> source) Returns the single element in the collection or throws. Iterator<? extends T> iterator = source.iterator(); if (!iterator.hasNext()) { throw new NoSuchElementException("Expecting collection with single element, got none."); T element = iterator.next(); if (iterator.hasNext()) { throw new IllegalArgumentException("Expecting collection with single element, got multiple."); return element; |
T[] | toArray(Iterable to Array LinkedList<T> accum = new LinkedList<T>(); for (T element : elements) { accum.add(element); return (T[]) accum.toArray(); |