Java tutorial
//package com.java2s; import java.util.Collection; import java.util.Iterator; public class Main { public static Object findOne(Collection coll) { if (coll.isEmpty()) { return null; } else if (coll.size() > 1) { throw new RuntimeException("Expected only one member in collection, found many: " + coll.toString()); } else { return coll.iterator().next(); } } public static String toString(Collection coll) { return toString(coll, ", "); } public static String toString(Collection coll, String delim) { StringBuffer sb = new StringBuffer(); for (Iterator it = coll.iterator(); it.hasNext();) { Object obj = it.next(); sb.append(String.valueOf(obj)); if (it.hasNext()) { sb.append(delim); } } return sb.toString(); } }