Java Iterable First getFirst(final Iterable iterable)

Here you can find the source of getFirst(final Iterable iterable)

Description

Gets the first element from an iterable.

License

Open Source License

Parameter

Parameter Description
T The type of element.
iterable The iterable to get the first element from.

Return

The first element from the iterable, if one exists. Otherwise, null.

Declaration

public static <T> T getFirst(final Iterable<? extends T> iterable) 

Method Source Code

//package com.java2s;
/*//w ww .ja v  a 2 s.  c om
 * File:                CollectionUtil.java
 * Authors:             Justin Basilico
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright March 25, 2008, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government. Export
 * of this program may require a license from the United States Government.
 * See CopyrightHistory.txt for complete details.
 *
 */

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Gets the first element from an iterable. If the iterable is null or
     * empty, null is returned.
     *
     * @param <T> The type of element.
     * @param iterable The iterable to get the first element from.
     * @return The first element from the iterable, if one exists. Otherwise,
     * null.
     */
    public static <T> T getFirst(final Iterable<? extends T> iterable) {
        if (iterable == null) {
            // No first element.
            return null;
        }

        final Iterator<? extends T> iterator = iterable.iterator();
        if (iterator.hasNext()) {
            return iterator.next();
        } else {
            // No first element.
            return null;
        }
    }

    /**
     * Gets the first element of the list. If the list is null or empty, null is
     * returned.
     *
     * @param <T> The type of element in the list.
     * @param list The list to get the first element from.
     * @return The first element from the list, if one exists. Otherwise, null.
     */
    public static <T> T getFirst(final List<? extends T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    /**
     * Returns true if the given collection is null or empty.
     *
     * @param collection The collection to determine if it is null or empty.
     * @return True if the given collection is null or empty.
     */
    public static boolean isEmpty(final Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Returns true if the given iterable is null or empty.
     *
     * @param iterable The iterable to determine if it is null or empty.
     * @return True if the given iterable is null or empty.
     */
    public static boolean isEmpty(final Iterable<?> iterable) {
        if (iterable == null) {
            // It is null, so it is empty.
            return true;
        } else if (iterable instanceof Collection) {
            return ((Collection<?>) iterable).isEmpty();
        } else {
            return !iterable.iterator().hasNext();
        }
    }
}

Related

  1. first(Iterable iterable)
  2. firstElement(Iterable iterable)
  3. firstElementOrNull(Iterable iterable)
  4. firstOf(final Iterable iterable)
  5. firstOrNull(Iterable iterable)
  6. getFirst(final Iterable iterable)
  7. getFirst(Iterable iterable)
  8. getFirst(Iterable c)
  9. getFirst(Iterable iterable)