Java Utililty Methods Iterable Item

List of utility methods to do Iterable Item

Description

The list of methods to do Iterable Item are organized into topic(s).

Method

booleanisPrefix(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;
...
booleaniterableContains(Iterable i, E match)
iterable Contains
for (E e : i) {
    if (Objects.equals(e, match)) {
        return true;
return false;
Collectionmaxes(Iterable elements)
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);
...
Iterableprefix(final Iterable lines, final String prefix)
prefix
return new Iterable<String>() {
    public Iterator<String> iterator() {
        return prefix(lines.iterator(), prefix);
};
Stringprint(Iterable coll, String sep)
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();
Iteratorreverse(Iterable values)
reverse
LinkedList<T> reversed = new LinkedList<>();
for (T value : values) {
    reversed.add(value);
return reversed.descendingIterator();
Tsingle(Iterable 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 elements)
to Array
LinkedList<T> accum = new LinkedList<T>();
for (T element : elements) {
    accum.add(element);
return (T[]) accum.toArray();