Java Iterable Item getFirstElement(Object maybeIterable)

Here you can find the source of getFirstElement(Object maybeIterable)

Description

Returns the first element, if any, of an object that may be an Iterable

License

Apache License

Parameter

Parameter Description
maybeIterable a parameter

Declaration

public static Object getFirstElement(Object maybeIterable) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//from   w ww  .  jav  a  2 s . com
     * Returns the first element, if any, of an object that may be an {@link Iterable}
     *
     * @param maybeIterable
     * @return
     */
    public static Object getFirstElement(Object maybeIterable) {
        if (!(maybeIterable instanceof Iterable<?>))
            return null;
        Iterable<?> iterable = (Iterable<?>) maybeIterable;
        Iterator<?> iterator = iterable.iterator();
        return iterator.hasNext() ? iterator.next() : null;
    }
}

Related

  1. concat(Iterable... iterables)
  2. concat(Iterable first, Iterable second)
  3. contains(Iterable iter, Object o)
  4. getFirst(final Iterable iterable)
  5. getFirst(Iterable iterable)
  6. getFrom(Iterable iterable, int index)
  7. isIterable(Object obj)
  8. isPrefix(Iterable suspectedPrefix, Iterable container)
  9. iterableContains(Iterable i, E match)