Here you can find the source of unique(Iterable
public static <T> T unique(Iterable<T> source, T defaultElement)
//package com.java2s; //License from project: Open Source License import java.util.Iterator; public class Main { /**/*w ww . j a v a 2 s.c o m*/ * Gets either the only element or the default one. If there are multiple elements in <tt>source</tt>, an * {@link IllegalStateException} is thrown. */ public static <T> T unique(Iterable<T> source, T defaultElement) { T element = defaultElement; if (source != null) { Iterator<T> i = source.iterator(); if (i.hasNext()) { element = i.next(); if (i.hasNext()) { throw new IllegalStateException("Element not unique!"); } } } return element; } }