Java Collection First firstElement(Collection in)

Here you can find the source of firstElement(Collection in)

Description

return the first element in a collection - this is most useful where only one item exists in the collection

License

Apache License

Parameter

Parameter Description
in non-null collection with at least one value

Exception

Parameter Description
IllegalArgumentException of the collection is empty

Return

non-null object

Declaration

public static Object firstElement(Collection in) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**//from   w  w w .j  av a  2 s.  c  om
     * return the first element in a collection - this is most useful where only one item
     * exists in the collection
     * @param in non-null collection with at least one value
     * @return non-null object
     * @throws IllegalArgumentException of the collection is empty
     */
    public static Object firstElement(Collection in) {
        if (in.size() == 0)
            throw new IllegalArgumentException("firstElement requies at least one entry");
        return (in.iterator().next());
    }
}

Related

  1. first(Collection self)
  2. first(Collection c)
  3. first(Collection c)
  4. first(Collection collection)
  5. first(Collection collection)
  6. firstElement(Collection c)
  7. firstElementOf(final Collection collection)
  8. firstElementOf(final Collection items)
  9. firstFrom(Collection collection)