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

Objectfirst(Collection collection)
first
Object[] obj = collection.toArray();
if (obj.length > 0) {
    return obj[0];
} else {
    return null;
Tfirst(Collection collection)
first
if (size(collection) > 0) {
    return (T) collection.iterator().next();
return null;
Objectfirst(Collection items)
Null safe access to the first object in a collection
if (items != null && items.size() > 0) {
    return items.iterator().next();
return null;
Efirst(Collection collection, E alternative)
first
Iterator<? extends E> iterator = collection.iterator();
if (iterator.hasNext()) {
    return iterator.next();
return alternative;
Tfirst(Collection collection)
first
ArrayList<T> list = new ArrayList<>(collection);
return list.isEmpty() ? null : list.get(0);
Tfirst(Collection collection)
first
final T notFound = null;
return isEmpty(collection) ? notFound : collection.iterator().next();
Doublefirst(Collection values)
first
List<Double> filteredValues = filterNulls(values);
return filteredValues.size() > 0 ? filteredValues.get(0) : null;
Efirst(Collection collection)
first
if (isEmpty(collection)) {
    return null;
Iterator<E> iterator = collection.iterator();
if (iterator.hasNext()) {
    return iterator.next();
throw new IllegalStateException("Should not happen");
...
Efirst(Collection self)
Implementation of the OCL
  • OrderedSet::first() : T
  • Sequence::first() : T
operations.
if (self.isEmpty()) {
    return null; 
return self.iterator().next();
Tfirst(Collection c)
first
return c.iterator().next();