Here you can find the source of getFirstAndOnly(Collection
public static <T> T getFirstAndOnly(Collection<T> c)
//package com.java2s; //License from project: Apache License import java.util.Collection; public class Main { public static <T> T getFirstAndOnly(Collection<T> c) { if (c.size() != 1) { throw new Error("Size must be 1 but was " + c.size()); }/* ww w . j a va 2 s .c om*/ return getFirst(c); } @SuppressWarnings("rawtypes") public static int size(Iterable<?> i) { if (i instanceof Collection) { return ((Collection) i).size(); } int size = 0; for (@SuppressWarnings("unused") Object o : i) { size++; } return size; } public static <T> T getFirst(Iterable<T> ts) { for (T t : ts) { return t; } throw new Error("collection has no first element"); } }