Java Utililty Methods Iterable

List of utility methods to do Iterable

Description

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

Method

TgetByIndex(Iterable iterable, int index)
get By Index
T el = null;
Iterator<T> it = iterable.iterator();
for (int i = 0; it.hasNext(); i++) {
    T cur = it.next();
    if (i == index) {
        el = cur;
        break;
return el;
DataTypegetElement(final Iterable iterable, int index)
Returns the indexed value into the Iterable .
if (iterable instanceof List<?>) {
    return ((List<DataType>) iterable).get(index);
} else {
    if (index < 0) {
        throw new IndexOutOfBoundsException("index must be >= 0");
    for (DataType v : iterable) {
        if (index == 0) {
...
TgetIdenticalElement(Iterable iterable)
get Identical Element
return getIdenticalElement(iterable.iterator());
StringgetLongestCommonToken(Iterable iterable, char tokenSeparatorChar)
get Longest Common Token
Iterator<String> it = iterable.iterator();
String commonToken = it.next();
while (it.hasNext()) {
    if (commonToken == "") {
        return null;
    String name = it.next();
    if (!name.startsWith(commonToken)) {
...
StringgetMessageString(Iterable msgs)
get Message String
String str = "";
Iterator iter = msgs.iterator();
while (iter.hasNext())
    str += iter.next() + ", ";
return str;
TgetUnique(Iterable iterable)
get Unique
final Iterator<? extends T> iter = iterable.iterator();
if (!iter.hasNext()) {
    throw new IllegalArgumentException("No element in " + iterable);
final T result = iter.next();
if (iter.hasNext()) {
    throw new IllegalArgumentException("More than (expected) one element in " + iterable);
return result;
StringimplodeStrings(Iterable strings, String glue)
implode Strings
StringBuffer stringBuffer = new StringBuffer();
for (Iterator<String> i = strings.iterator(); i.hasNext();) {
    String varClassName = i.next();
    stringBuffer.append(varClassName);
    if (i.hasNext()) {
        stringBuffer.append(glue);
return stringBuffer.toString();
intindexOfSame(Iterable iterable, T obj)
index Of Same
int ix = 0;
Iterator<? extends T> iterator = iterable.iterator();
while (iterator.hasNext()) {
    T element = iterator.next();
    if (element == obj)
        return ix;
    ix++;
return -1;
voidinternalSeparate(StringBuilder buff, Iterable args, String separator, String useWhenNone)
internal Separate
Iterator<?> i = args.iterator();
if (i.hasNext()) {
    for (;;) {
        Object thing = i.next();
        buff.append(thing.toString());
        if (i.hasNext()) {
            buff.append(separator);
        } else
...
booleanisEmpty(Iterable iterable)
 isEmpty( null ) = true isEmpty( Arrays.asList()) = true isEmpty( Arrays.asList("") ) = false 
boolean retval = true;
if (iterable != null) {
    Iterator<?> iterator = iterable.iterator();
    retval = iterator == null || !iterator.hasNext();
return retval;