Java Collection First getFirst(Collection collection)

Here you can find the source of getFirst(Collection collection)

Description

Utility function to get a sample item from a collection

License

Open Source License

Parameter

Parameter Description
collection a parameter

Return

the first item from the collection, or null if the collection is empty

Declaration

public static String getFirst(Collection<String> collection) 

Method Source Code

//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/*w  w w. j  a  va2 s.  c  om*/
     * Utility function to get a sample item from a collection
     *
     * @param collection
     * @return the first item from the collection, or null if the collection is
     * empty
     */
    public static String getFirst(Collection<String> collection) {
        Iterator<String> it = collection.iterator();
        if (it.hasNext()) {
            return it.next();
        }
        return null;
    }
}

Related

  1. firstValue(Class type, Collection values)
  2. GET_FIRST_ELEMENT(Collection coll)
  3. getCollectionFirstElement( final Collection collection)
  4. getFirst(Collection c)
  5. getFirst(Collection c)
  6. getFirst(Collection bag)
  7. getFirst(Collection c)
  8. getFirst(Collection col)
  9. getFirst(Collection coll)