Java tutorial
//package com.java2s; import java.util.Collection; import java.util.HashSet; import java.util.Set; public class Main { @SuppressWarnings({ "rawtypes", "unchecked" }) public static boolean containsOnly(Collection collection, Object object) { if (collection == null) return false; Set duplicatesRemoved = new HashSet(collection); return duplicatesRemoved.size() == 1 && duplicatesRemoved.contains(object); } }