Java Iterable First 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.Iterator;

public class Main {
    /**//from  w w w  . j  av  a2  s.  co  m
     * 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. getFirst(Iterable c)
  2. getFirst(Iterable iterable)
  3. getFirst(Iterable iterable)
  4. getFirst(Iterable iterable)
  5. getFirst(Iterable iterable, T defaultValue)
  6. getFirstOrNull(Iterable it)
  7. getFirstOrNull(Iterable iterable)
  8. removeFirst(Iterable iterable)