Here you can find the source of first(Collection
public static <E> E first(Collection<E> collection)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static <E> E first(Collection<E> collection) { if (isEmpty(collection)) { return null; }// w ww. jav a 2 s .co m Iterator<E> iterator = collection.iterator(); if (iterator.hasNext()) { return iterator.next(); } throw new IllegalStateException("Should not happen"); } private static <E> boolean isEmpty(Collection<E> collection) { return (collection == null || collection.isEmpty()); } private static <E> boolean isEmpty(E[] array) { return (array == null || array.length == 0); } }