Java Collection First getFirstItem(Collection c)

Here you can find the source of getFirstItem(Collection c)

Description

Returns the first item in the collection or null if the collection is empty

License

Open Source License

Declaration

public static <X> X getFirstItem(Collection<X> c) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**/*w ww.ja  va 2 s .  co m*/
     * Returns the first item in the collection or null if the collection is empty
     */
    public static <X> X getFirstItem(Collection<X> c) {
        X o;
        if (c == null || c.isEmpty()) {
            o = null;
        } else if (c instanceof List) {
            o = ((List<X>) c).get(0);
        } else {
            o = c.iterator().next();
        }
        return o;
    }
}

Related

  1. getFirstElement(Collection collection, T defaultValue)
  2. getFirstElementOrNull(Collection collection)
  3. getFirstElementOrNull(Collection coll)
  4. getFirstItem(Collection c)
  5. getFirstItem(Collection collection)
  6. getFirstItemInCollection(Collection collection)
  7. getFirstN(Collection objects, int n)
  8. getFirstNonNull(Collection c)
  9. getFirstNotNullValue(final Collection collection)