Java Utililty Methods Collection First

List of utility methods to do Collection First

Description

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

Method

EfirstNonNull(Collection collection)
first Non Null
return firstNonNull(collection, null);
TfirstOrThat(Collection collection, T that)
first Or That
if (collection == null || collection.size() == 0) {
    return that;
return collection.iterator().next();
TfirstValue(Class type, Collection values)
first Value
final T value = findFirstValue(type, values);
if (null != value) {
    return value;
throw new IllegalStateException("Expecting at least one value");
ObjectGET_FIRST_ELEMENT(Collection coll)
Returns the first element of the given collection.
Object ret = null;
if (coll.size() > 0) {
    Object[] ar = new Object[coll.size()];
    ar = coll.toArray(ar);
    for (int i = 0; i < ar.length; i++) {
        if ((ar[i] != null)) {
            ret = ar[i];
            break;
...
TgetCollectionFirstElement(final Collection collection)
Gets the first element of a collection, if it exist.
T result = null;
if (collection.isEmpty() == false) {
    result = new ArrayList<T>(collection).get(0);
return result;
ObjectgetFirst(Collection c)
Returns the first element in a collection (according to its iterator).
return c.iterator().next();
ObjectgetFirst(Collection c)
get First
if (c == null || c.isEmpty()) {
    return null;
return c.iterator().next();
StringgetFirst(Collection collection)
Utility function to get a sample item from a collection
Iterator<String> it = collection.iterator();
if (it.hasNext()) {
    return it.next();
return null;
TgetFirst(Collection bag)
get First
if (bag.isEmpty())
    return null;
if (bag instanceof java.util.List) {
    return ((List<T>) bag).get(0);
} else {
    return bag.iterator().next();
TgetFirst(Collection c)
get First
return getFirst(c.iterator());