Java Collection First first(Collection collection)

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

Description

first

License

Open Source License

Declaration

public static <E> E first(Collection<E> collection) 

Method Source Code


//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);
    }
}

Related

  1. first(Collection items)
  2. first(Collection collection, E alternative)
  3. first(Collection collection)
  4. first(Collection collection)
  5. first(Collection values)
  6. first(Collection self)
  7. first(Collection c)
  8. first(Collection c)
  9. first(Collection collection)