Java Iterable getIdenticalElement(Iterable iterable)

Here you can find the source of getIdenticalElement(Iterable iterable)

Description

get Identical Element

License

Open Source License

Declaration

public static <T> T getIdenticalElement(Iterable<T> iterable) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    /**/* w w w.j  ava  2 s  . co m*/
     * @see JavaHelper#getIdenticalElement(Iterator)
     */
    public static <T> T getIdenticalElement(Iterable<T> iterable) {
        return getIdenticalElement(iterable.iterator());
    }

    /**
     * If all elements in the iterator are (equal-)identical, return the element
     * (the first). Else return <code>null</code>.
     */
    public static <T> T getIdenticalElement(Iterator<T> iterator) {
        if (!iterator.hasNext()) {
            return null;
        }

        final T result = iterator.next();

        while (iterator.hasNext()) {
            T next = iterator.next();
            if (!result.equals(next)) {
                return null;
            }
        }

        return result;
    }
}

Related

  1. equalsIterablesInOrder(Iterable i1, Iterable i2)
  2. fillFromIterable(C c, Iterable i)
  3. getAny(Iterable iterable)
  4. getByIndex(Iterable iterable, int index)
  5. getElement(final Iterable iterable, int index)
  6. getLongestCommonToken(Iterable iterable, char tokenSeparatorChar)
  7. getMessageString(Iterable msgs)
  8. getUnique(Iterable iterable)
  9. implodeStrings(Iterable strings, String glue)