Here you can find the source of getFirst(Iterable extends T> iterable)
Parameter | Description |
---|---|
NoSuchElementException | if the iterable is empty |
public static <T> T getFirst(Iterable<? extends T> iterable)
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.NoSuchElementException; public class Main { /**//from ww w. j ava2 s .co m * Return the first element in the given (non-empty) {@link Iterable} * @throws NoSuchElementException if the iterable is empty */ public static <T> T getFirst(Iterable<? extends T> iterable) { if (iterable instanceof List) { final List<T> list = (List<T>) iterable; if (list.isEmpty()) throw new NoSuchElementException(); return list.get(0); } else { return iterable.iterator().next(); } } }