Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.NoSuchElementException; public class Main { public static <T> T only(Collection<T> coll) { if (coll.size() != 1) { throw new NoSuchElementException("Collection has none or more than one elements"); } return coll.iterator().next(); } }